C 从数组中获取单个元素?
我试图一次打印C 从数组中获取单个元素?,c,C,我试图一次打印s数组中的每个char。程序我已经打印了数组中定义索引后的所有字母 我面临的问题是,在用户插入短语之后,它就被分配了。如何使用指针从字符串中调用字母 你能用strtok来分割这个字符串吗?我试着把delim设置为“”没有输出。 换句话说,让它成为一个类似Python的列表split() #包括 #包括 #包括 #包括 int main(){ char*s; s=malloc(1024*sizeof(char)); scanf(“%[^\n]”,s); s=realloc(s,str
s
数组中的每个char
。程序我已经打印了数组中定义索引后的所有字母
我面临的问题是,在用户插入短语之后,它就被分配了。如何使用指针从字符串中调用字母
你能用strtok来分割这个字符串吗?我试着把delim设置为“”没有输出。
换句话说,让它成为一个类似Python的列表split()
#包括
#包括
#包括
#包括
int main(){
char*s;
s=malloc(1024*sizeof(char));
scanf(“%[^\n]”,s);
s=realloc(s,strlen(s)+1;
//printf(“%s\n”,s);
对于(int i=0;i
或
len=strlen;
对于(int i=0;i
为什么要用%s
打印单个字符?用于(int i=0;s[i]!='\0';i++)printf(“%c\n”,s[i”);
?我应该用什么,%ch
?你应该用%c
和s[i]
,而不是&s[i]
你能解释一下第二条printf
因为s[i]=*(s+i)
它们是等价的。s具有数组第一个元素的地址,可以将i个位置相加,然后访问存储在该位置的值。
#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));
len = strlen(s);
for (int i = 0 ; i < len; i++){
printf("%c\n", s[i]);
}
int main()
{
char *s;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
for (int i = 0; s[i] != '\0'; i++)
{
printf("%c \n", s[i]);
}
return 0;
}