how to make an array of strings

This is a discussion on how to make an array of strings within the C Programming forums, part of the General Programming Boards category; I know a string is just an array of chars, but how can I make an array of strings? Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    how to make an array of strings

    I know a string is just an array of chars, but how can I make an array of strings?
    Code:
    #include <stdio.h>
    #define length 80
    
    void main(void) {
    
    	FILE *fp;
    	int ch;
    	char *str;
    	int i=0;
    
    	if ((fp=fopen("myfile.txt","r")) == NULL) {
    		printf(stderr,"fopen: Error opening file\n");
    		exit(1);
    	}
    
    	while (!feof(fp)) {
    		fgets(str, length, fp);
    		printf("%s",str);
    		fflush(str);
    
    	}
    
    	fclose(fp);
    	exit(0);
    }
    The above prints out the each line like it's supposed to, now if I try this:
    Code:
                    char *str[10];
    .................................................
    	while (!feof(fp)) {
    		fgets(str[i], length, fp);
    		printf("%s",str[i]);
    		fflush(str[i]);
            i++;
    	}
    I get segmentation faults. The purpose is not actually to print the lines, but to actually manipulate them first and then print to the screen which is why an array would help. Any suggestions? Thanks.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>how can I make an array of strings?
    Just like you make an array of integers/floats but with a '\0' null at the end of the string.

    >>void main(void)
    this is wrong, main only returns int, so change this to int main() and return a value at the end of the program.

    >>while (!feof(fp))
    read this

    >>fgets(str, length, fp);
    remember, str is a pointer, not a string, if you want to save data into str, allocate memmory for it, using malloc or what you want, or simple just declare it as: char str[BUFSIZ]; you BUFSIZ is declared in stdlib, if you want to change, change to a size to suit your needs.

    >>printf(stderr,"fopen: Error opening file\n");
    I think you mean fprintf.
    Last edited by Vber; 03-24-2003 at 10:23 AM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Thanks a lot. This helps. I do apologize for asking easy questions, but I just started coding C a week ago. Vber, you were right about that fprint as well. Salem, I originally had fflush(fp) but I was getting the last line printed twice which was explained in the FAQ but my TA didn't know why and said to try fflush(str) and although it gave me a warning it actually fixed that last line printing twice problem, however I did use the correction in the FAQ as it is obviously the better way to do it. Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  2. Worksafe Array of Strings
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 03-28-2008, 11:00 PM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21