Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言在循环中从数组打印字符串_C_Arrays_String_Visual Studio 2015 - Fatal编程技术网

C语言在循环中从数组打印字符串

C语言在循环中从数组打印字符串,c,arrays,string,visual-studio-2015,C,Arrays,String,Visual Studio 2015,我正在使用visualstudio 2015用C语言制作一个控制台应用程序,这样用户就可以输入他们想要制作的糖果的数量和糖果的名称。然而,问题是,当程序试图从数组中读取字符串时,程序崩溃,并且不打印任何内容,但是,如果我将其更改为使用%c打印单个字符,它将打印出字符串的第一个字符,例如,如果我输入2个sweets,字符串“Jawbreaker”和“Lollipop”,则如果我使用%s作为字符串,它将崩溃。但是,如果我使用%c作为字符,它将完成其工作,并在不同的行上分别打印“J”和“L”,您知道如

我正在使用visualstudio 2015用C语言制作一个控制台应用程序,这样用户就可以输入他们想要制作的糖果的数量和糖果的名称。然而,问题是,当程序试图从数组中读取字符串时,程序崩溃,并且不打印任何内容,但是,如果我将其更改为使用%c打印单个字符,它将打印出字符串的第一个字符,例如,如果我输入2个sweets,字符串“Jawbreaker”和“Lollipop”,则如果我使用%s作为字符串,它将崩溃。但是,如果我使用%c作为字符,它将完成其工作,并在不同的行上分别打印“J”和“L”,您知道如何使用字符串的%s说明符来实现此功能吗

代码如下:

#include <stdio.h>
/*Declares the variable needed*/
int sweet_number;
char sweet_name[999];


int main(void) {

/*Prompts the user to enter the number of sweets and saves it to sweet_number*/
printf("Please enter the number of sweets:\n");
scanf("%d", &sweet_number);

/*for loop to enter the name of the sweet into the array*/ 
for (int i = 0; sweet_number > i; i++) {                  
    printf("What is the name of the sweet?\n");
    scanf("%s", &sweet_name[i]);
    }

/*Prints array to screen*/
for (int j = 0; sweet_number > j; j++){   /* <- This is where code fails to run*/
    printf("%s\n", sweet_name[j]);
}

return 0;

}
#包括
/*声明所需的变量*/
整数;
char sweet_name[999];
内部主(空){
/*提示用户输入糖果的数量,并将其保存到sweet\u number*/
printf(“请输入糖果的数量:\n”);
scanf(“%d”和sweet_编号);
/*for循环在数组中输入糖果的名称*/
对于(int i=0;sweet_number>i;i++){
printf(“糖果的名称是什么?\n”);
scanf(“%s”和sweet_name[i]);
}
/*将阵列打印到屏幕*/

对于(int j=0;sweet_number>j;j++){/*您必须使用二维数组。
sweet_name
是一个数组(1-D),每个索引最多只能存储一个字符,而不是字符串。更改以下行

char sweet_name[999];


好的,我建议你做一些更有效的事情,那就是使用双指针。通过这样做,你可以解决2D数组版本存在的一些问题。
第一个问题是,如果用户想要插入超过999个糖果,您该怎么办?您的阵列无法容纳它们。
第二,如果用户输入的名称大于100个字符,你该怎么办?同样,你的2D数组无法容纳它。而且,尽管用户输入的名称可能大于100个字符,但大多数用户输入的名称都会大大少于100个字符。现在,对于每个字符串,你已经分配了100个位置,而你可能只有一个艾德大约50岁。
那么,让我们来解决这些问题。 我可能会这样做:

#include <stdio.h>
#include <string.h> // for strcpy(), strlen()
#include <stdlib.h> // for malloc()

int main(void) {    

char **sweet_names;  // make a double pointer(or you might see that as array of pointers
char reader[300];   // this variable will help us read every name into the sweet_names
int sweet_number;
int i, j;

// Your code here to get the number of sweet_names
/*Prompts the user to enter the number of sweets and saves it to sweet_number*/
printf("Please enter the number of sweets:\n");
scanf("%d", &sweet_number);

// Now that you know the sweet_number, allocate as much memory as you need.
// And that can be more than 999 sweet names
sweet_names = (char **) malloc(sweet_number * sizeof(char *));
// i.e. make a character pointer to sweet_number character pointers.

// Again, some of your code here
for (i = 0; sweet_number > i; i++) {                  
     printf("What is the name of the sweet?\n");
     scanf("%s", reader);   // read into the reader
     sweet_names[i] = (char *) malloc(strlen(reader) + 1); // allocate as much memory as you need for the current string, the one just read into reader
     strcpy(sweet_names[i], reader);   // copy contents of reader to sweet_names[i]
}

// Again, your code to print the names
for (j = 0; sweet_number > j; j++){   
    printf("%s\n", sweet_names[j]);
    free(sweet_names[j]);  // free every string you allocated, it is good practice
}

// Finally, free the sweet_names pointers. Generally, when you allocate
// dynamically, which is what we do with malloc(), it is a good practice
// to free what you allocate becuase otherwise stays in memory and then
// memory leaks are created. There is a lot to say about C and memory
// but anyway, remember to free
free(sweet_names);

return 0;

}
#包括
#包含//用于strcpy(),strlen()
#包含//用于malloc()
int main(void){
char**sweet_names;//创建一个双指针(或者您可能将其视为指针数组
char reader[300];//此变量将帮助我们将每个名称读入sweet_名称中
整数;
int i,j;
//你的代码在这里可以得到甜心的名字
/*提示用户输入糖果的数量,并将其保存到sweet\u number*/
printf(“请输入糖果的数量:\n”);
scanf(“%d”和sweet_编号);
//既然您知道了sweet_编号,请分配所需的内存。
//这可能超过999个甜蜜的名字
sweet_name=(char**)malloc(sweet_number*sizeof(char*);
//也就是说,创建一个指向sweet_数字字符指针的字符指针。
//同样,这里有一些代码
对于(i=0;sweet_number>i;i++){
printf(“糖果的名称是什么?\n”);
scanf(“%s”,读卡器);//读入读卡器
sweet_names[i]=(char*)malloc(strlen(reader)+1);//为当前字符串(即刚刚读入reader的字符串)分配所需的内存
strcpy(sweet_names[i],reader);//将读取器的内容复制到sweet_names[i]
}
//同样,您可以使用代码打印名称
对于(j=0;sweet_数>j;j++){
printf(“%s\n”,sweet_name[j]);
free(sweet_names[j]);//释放分配的每个字符串,这是一个很好的实践
}
//最后,释放sweet_names指针。通常,当您分配
//动态地,这就是我们对malloc()所做的,这是一个很好的实践
//释放分配的内容,否则将保留在内存中,然后
//内存泄漏是被创建的。关于C和内存有很多要说的
//但不管怎样,记住要自由
免费(甜蜜的名字);
返回0;
}
最后,由于
阅读器
,现在程序又有了最多300个字符的只读名称限制,但这也是您可以处理的事情,这就是为阅读器分配大量内存,比如1000000
字符,然后将其释放。

您想要糖果名称的第一个字符还是全名?非常感谢您解决了我的问题!这正是我想要的:我很高兴它能帮助您。:@Henry,别忘了将
scanf(“%s”)和scanf(“%s”,sweet_name[I])
谢谢您提供的信息,这超出了我的项目范围,但我会保存它以备将来参考!欢迎您!类似的字符串操作方法,顺便说一句,在C中这是一个大概念,您会经常看到。对于您的项目,BeingMIAkashs的答案可能更好,因为它简单,但我认为它会很好我想展示一些字符串操作的替代方案。
#include <stdio.h>
#include <string.h> // for strcpy(), strlen()
#include <stdlib.h> // for malloc()

int main(void) {    

char **sweet_names;  // make a double pointer(or you might see that as array of pointers
char reader[300];   // this variable will help us read every name into the sweet_names
int sweet_number;
int i, j;

// Your code here to get the number of sweet_names
/*Prompts the user to enter the number of sweets and saves it to sweet_number*/
printf("Please enter the number of sweets:\n");
scanf("%d", &sweet_number);

// Now that you know the sweet_number, allocate as much memory as you need.
// And that can be more than 999 sweet names
sweet_names = (char **) malloc(sweet_number * sizeof(char *));
// i.e. make a character pointer to sweet_number character pointers.

// Again, some of your code here
for (i = 0; sweet_number > i; i++) {                  
     printf("What is the name of the sweet?\n");
     scanf("%s", reader);   // read into the reader
     sweet_names[i] = (char *) malloc(strlen(reader) + 1); // allocate as much memory as you need for the current string, the one just read into reader
     strcpy(sweet_names[i], reader);   // copy contents of reader to sweet_names[i]
}

// Again, your code to print the names
for (j = 0; sweet_number > j; j++){   
    printf("%s\n", sweet_names[j]);
    free(sweet_names[j]);  // free every string you allocated, it is good practice
}

// Finally, free the sweet_names pointers. Generally, when you allocate
// dynamically, which is what we do with malloc(), it is a good practice
// to free what you allocate becuase otherwise stays in memory and then
// memory leaks are created. There is a lot to say about C and memory
// but anyway, remember to free
free(sweet_names);

return 0;

}