Tag: c strings

当第一个早期定义的字符串数组的字符串为null时,scanf()不读取输入字符串

我为字符串定义了一个数组。 如果我以第一个元素不是空字符串的方式定义它,它工作正常。 当它是一个空字符串时,另一个字符串的下一个scanf()停止读取输入字符串,程序停止执行。 现在我不明白如何定义字符串数组会影响scanf()读取输入。 char *str_arr[] = {“”,”abc”,””,””,”b”,”c”,””,””,””}; // if first element is “abc” instead of “” then works fine int size = sizeof(str_arr)/sizeof(str_arr[0]); int i; printf(“give string to be found %d\n”,size); char *str; scanf(“%s”,str); printf(“OK\n”);

如何在C中为char **动态分配内存

我将如何在此函数中动态分配内存到char **列表? 基本上这个程序的想法是我必须从文件中的单词列表中读取。 我不能假设最大字符串或最大字符串长度。 我必须用C字符串做其他的东西,但那些东西我应该没问题。 谢谢! void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile = fopen(argv[1], “r”); if(!myFile) { printf(“No such file or directory\n”); exit(EXIT_FAILURE); } while((c = fgetc(myFile)) !=EOF) { numberOfChars++; […]

C中的二维数组字符

帮助我摆脱这个问题。 我在ubuntu12.04上使用GCC。 当我编写这个程序从键盘n获取5个字符串然后在屏幕上打印这些字符串。 程序已编译但在执行期间它从键盘获取字符串但仅打印最后一个字符串。 我写的程序如下: void main() { char names[10]; int i,j; for(i=0;i<5;i++) { printf(" Enter a name which you want to register\n"); scanf("%s",names); } for(i=0;i<5;i++) printf(" the names you enter are %s\n", names); }

为什么在c ++中使用c字符串?

现在有没有什么好的理由在C ++中使用C-strings? 我的教科书在某些方面的例子中使用它们,我觉得使用std :: string会更容易。

在C中,我可以在指针声明中初始化字符串,就像我可以在char数组声明中初始化字符串一样吗?

这两行代码是否实现了相同的结果? 如果我在函数中有这些行,那么在两种情况下字符串都存储在堆栈中吗? 除了不需要在第一行代码中声明空终止符之外,我是否应该使用一个而不是另一个? char s[] = “string”; char* s = “string\0”;

scanf和strcmp with c string

我找到了一个很好的例子 ,说明如何使用strcmp,但它只适用于fgets(),我需要使用scanf。 所以,这是代码: int main(void) { char fruit[] = “apple\n”; char ans[80]; do { printf (“Guess my favorite fruit? “); scanf (“%s”,ans); } while (strcmp (fruit, ans) != 0); puts (“Correct answer!”); return 0; } 即使我写了正确的answear(“苹果”),它仍然留在循环中并一直问我最喜欢的水果……我猜它与没有写在ans [80]的字符有关。 (我需要它是一个最多80chars的char数组)。 我不是这个…… 提前致谢。

C中的fwrite在输出文件中给出不同的值

当我在同一个函数中的另一个函数VERSUS fwrite中使用fwrite时,为什么输出文件不同? output1.txt包含像Ê这样的垃圾值,这是不正确的 output2.txt包含“b”,这是正确的 #include #include void writeData(char *buf, char *path) { FILE *fp1; fp1 = fopen(path, “a”); fwrite(&buf, sizeof(char), strlen(buf), fp1); } int main () { char buf[2] = “a”; char buf2[3] = “yb”; char file1_path[12] = “output1.txt”; char file2_path[12] = “output2.txt”; memset(buf, 0, sizeof(buf)); memcpy(buf, &buf2[1], strlen(buf2)); printf(“%s\n”, buf); writeData(buf, file1_path); FILE *fp2; […]

char 初始化并处理它

我已经定义了一个char数组: char d[6]; 如果我对以下内容有误,请纠正我: 此时没有为变量d分配内存。 现在我要初始化它: d=”aaaaa”; 在这种初始化之后,就没有必要释放内存; 它将自动完成。 我如何知道char[]已初始化? 我正在寻找像这样的模式 if (filled(d)){..} 另外,如何用一种角色填充char []?

实现`strtok`,其分隔符有多个字符

代码段: char str[] = “String1::String2:String3:String4::String5”; char *deli = “::”; char *token = strtok(str,deli); while(token != NULL) { printf(“Token= \”%s\”\n”, token); token=strtok(NULL,deli); } 上面的代码片段产生输出: Token=”String1″ Token=”String2″ Token=”String3″ Token=”String4″ Token=”String5″ 但我希望输出为: Token=”String1″ Token=”String2:String3:String4″ Token=”String5″ 我知道我没有得到预期的输出,因为strtok的第二个参数中的每个字符都被视为分隔符。 为了获得预期的输出,我编写了一个程序,它使用strstr (以及其他东西)将给定的字符串拆分为标记,以便获得预期的输出。 这是程序: #include #include #include #include int myStrtok(char* str,char* deli) { if(str==NULL || deli==NULL) return -1; int tokens=0; char *token; char […]

为什么我不能在char *中编辑char?

下面是一个非常简单的例子。 它在Mac OS X(Snow Leopard)上使用gcc编译得很好。 在运行时它输出总线错误:10。这里发生了什么? char* a = “abc”; a[0] = ‘c’;