I am studying 'The C Programming Language' by Brian Kerningham & Dennis Ritchie.
I'm stuck on 1.9 Character Arrays.
I'm trying to allow the user to input multiple lines of text into the CMD that will then be stored in an array of strings parameter for use by another program. Each new line should be stored as a new object in the array. For now all I want to do is print the Array back to the CMD so I can see its working correctly, any ideas what i am doing wrong?
#include <stdio.h>
int main(char string[])
{
int c, i;
char * strs[i];
for (i=0; i<5 && (c!=EOF()) && c!='\n'; i++){
strs[i] = c;
}
for(i=0; i<5; ++i)
puts(strs[i]);
}
Your code has quite a number of mistakes in it.
main()
prototype is wrong, it should be int main(int argc, char *argv[]);
or something equivalent. Dropping the initial int
argument is not OK. strs
as an array of character pointers (without a valid size!), you probably want a full 2D array of chars, like char strs[100][32];
. Limited, but simpler to manage. strs[i] = c;
should be something like strs[i][j++] = c;
. Of course you must also respect the maximum length for each string, and terminate the string properly. i
) on newline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.