Thread: Array of strings problems...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Array of strings problems...

    Hello. I have a problem with an array of strings. I am scanning a string in a loop. I'm then assigning that string to an array of strings. The problem is, it just assigns a pointer. So when I try to look at the array later, It only displays the last modified string. So I need a way to assign the characters from a string to the array.

    ALSO, when I try to read in DIRECTLY to my array of strings, I get a "segmentation fault" error. You can see it is commented out on my code, but feel free to uncomment it and try.

    This could have something to do with my declaration, or the way I use my pointers. Pointers are confusing to me. Anyway, thanks for your help

    Source:

    Code:
    #include <stdio.h>
    
    #define MAXLETTERS 10
    #define MAXARRAYS 5
    
    int main(void)
    {
    	char array[MAXLETTERS];
    	char *list[MAXARRAYS];
    	int i;
    	for (i = 0; i < MAXARRAYS; i++)
    	{
    		scanf("%s",array);
    		//scanf("%s",list[i]); For some reason this doesn't work
    
    		list[i] = array; //This makes all arrays in the list point to the final one i typed in
    		printf("\narray: %s",array); //Prints the array we input
    		printf("\nlist %d: %s\n",i,list[i]); //Shows this array has been correctly copied
    	}
    
    	for (i = 0; i < MAXARRAYS; i++)
    	{
    		printf("%s\n",list[i]);
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to either allocate memory with malloc(), or you need to make your list a 2D array.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    char *list[MAXARRAYS]; is an array of pointers, but none of those pointers have any memory assigned to them. They are just like this:

    Code:
    char *ptr;
    To assign them memory, you could use malloc(). But in this case maybe what you want is:
    Code:
    char list[MAXARRAYS][MAXLETTERS];
    Which will give you MAXARRAYS arrays, each of MAXLETTERS length.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>scanf("%s",list[i]); For some reason this doesn't work
    SourceForge.net: Common mistakes and errors - cpwiki

    And your current design is also flawed. You are merely assigning a pointer to the list pointing to your buffer which is overwritten every time. You had better read up a little on strings and pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    Yeah, I just read some of those stupid mistakes on sourceforge. I can't believe I made them. My programming class didn't really go over "malloc" but now I get it. Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  3. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM