[英]Printing strings from 2D array using pointer to pointer
I am trying to build a program that uses dynamic allocation to build an array of strings. 我正在尝试构建一个使用动态分配来构建字符串数组的程序。 After the user finishes to enter the words he wants into the array i want to print the array one word after the other.
用户完成将要输入的单词输入数组后,我要一个接一个地打印该单词。 I am using pointers to pointers, however it doesn't seem to work:
我正在使用指向指针的指针,但是它似乎不起作用:
#define SIZE 256
void paintWords(char **words, int count_words);
void main() {
char **words = NULL;
int flag = 1;
char buffer[SIZE];
int count_words = 0;
char *curr_word;
while (flag)
{
_flushall();
printf("Enter a word:");
gets(buffer);
words = (char**)realloc(words,++count_words*sizeof(char*));
curr_word = (char*)malloc(strlen(buffer) + 1);
words[count_words - 1] = curr_word;
printf("Do you wish to continue(0-no, 1-yes):");
scanf("%d", &flag);
}
paintWords(words, count_words);
}
void paintWords(char **words, int count_words) {
int j = 0;
for (int i = 0; i < count_words; i++)
{
printf("%s\n", words[i][j]);
}
}
Copy buffer
to your malloc
'ed block with strcpy
使用
strcpy
将buffer
复制到您的malloc
块
strcpy(curr_word, buffer);
you are discarding the read word since you don't put it anywhere 您正在丢弃已读单词,因为您没有将其放置在任何地方
Don't use gets
use fgets
instead 不要使用
gets
使用fgets
代替
fgets(buffer, sizeof(buffer), stdin);
this would prevent a buffer overflow. 这样可以防止缓冲区溢出。
This is just the j
st which in your case is the 0
th character of the word 这仅仅是
j
ST而你的情况是0
字的个字符
printf("%s\\n", words[i][j]);
change it to 更改为
printf("%s\\n", words[i]);
turn compiler warnings on, it would tell you about printf
expecting a char *
and recieving char
instead. 打开编译器警告,它将告诉您有关
printf
期望使用char *
并接收char
。
Also consider the following: 还请考虑以下事项:
main()
should return int
. main()
应该返回int
。 malloc
. malloc
。 realloc
, use a temporary pointer and assign it to array
on success only. realloc
覆盖指针,只能使用临时指针,只有在成功时才将其分配给array
。 Otherwise if realloc
returns NULL
you will not be able to free(array)
for example. realloc
返回NULL
您将无法free(array)
。 ++count_words
words = realloc(words,count_words*sizeof(char*));
words[count_words-1] = malloc(strlen(buffer) + 1);
strcpy(words[count_words-1],buffer);
Later print the array 稍后打印数组
printf("%s\n",words[i]);
realloc()
can fail so realloc()
可能失败,所以
char *temp = realloc(words,count_words*sizeof(char*));
if(temp != NULL)
words = temp;
Few other fixes will be 其他修复将很少
You shouldn't be using gets
which is no more a standard. 您不应该使用不再是标准的
gets
。 Use fgets()
and note that fgets()
comes with a newline character 使用
fgets()
并注意fgets()
带有换行符
Check the code below: 检查以下代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 256
void paintWords(char **words, int count_words);
void main() {
char **words = NULL,ch;
int flag = 1;
char buffer[SIZE];
int count_words = 0;
//char *curr_word;
while (flag)
{
printf("Enter a word:");
fgets(buffer,sizeof(buffer),stdin);
words = (char**)realloc(words,++count_words*sizeof(char*));
words[count_words - 1] = (char*)malloc(strlen(buffer) + 1);
strcpy(words[count_words-1],buffer);
printf("Do you wish to continue(0-no, 1-yes):");
scanf("%d", &flag);
while((ch = getchar()) != '\n');
}
paintWords(words, count_words);
}
void paintWords(char **words, int count_words) {
int i;
for (i=0; i < count_words; i++)
{
printf("%s", words[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.