c - 从数组中获取单个元素?
问题描述
我正在尝试一次打印一个char
数组s
。该程序我打印了数组中定义的索引之后的所有字母。
我面临的问题是用户插入短语然后分配它。如何使用指针从字符串中调用字母?
你能用它strtok
来分割这个字符串吗,我试着把它设置delim
为“”没有输出。换句话说,让它像 Python 一样成为一个列表split()
。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
//printf("%s \n", s);
for (int i = 0 ; i <strlen(s); i++){
printf("%s \n", &s[i]);
}
return 0;
}
解决方案
printf("%c\n", s[i]);
或者
printf("%c\n", *(s + i));
推荐阅读
- angularjs - 添加元素后刷新 ng-repeat 列表
- symfony - 尝试查找实体时的循环引用
- android - 加载资源失败,没有异常详细信息
- python - 在 Python 3 Flask-Restful API 中使用 POSTMAN 在 POST 方法中获取数组键
- android - 使用 volley 添加标记以从 mysql 映射
- python-3.x - Flask-Admin 在数据库中存储 NULL 而不是空字符串 ('')
- python-3.x - 在两种颜色之间寻找一种颜色(算法)
- javascript - 格式数字在 ag 网格 5210.15 到 5,210.15
- svg - 如何在 SVG 中跟踪开放路径的一个边缘
- javascript - chartjs-plugin-zoom 在离线环境 javascript .js 上不起作用