Thread: help.. string array blows up my pgm

  1. #1
    Registered User
    Join Date
    Jun 2014
    Location
    Texas
    Posts
    6

    help.. string array blows up my pgm

    I'm using codeblocks and I defined a string array..
    Code:
     char *sname[10];   /* array for student name   */
    I loaded the array..
    Code:
    for ( x = 0; x < ngo; x++ )          // gets info*/
     {
       printf( "\nPlease enter the student name %d ", x);
       scanf("%s", &name);
       sname[x] = name;
       printf( "\nPlease enter the student gpa %d ", x);
       scanf("%d", &sgpa);
       gpa[x] = sgpa;
     }
    and then if I try to access it while sorting
    Code:
           printf("\n got here to name swap = %d %d %s ", i, j, sname[i]);
           holdname = sname[i];                                              /* swap the name  */
           sname[i] = sname[j];
           sname[j] = holdname;
    It fails and gives 'Segmentation fault."

    It is NOT the integer (it's well inside the boundary for the 10, but if I try to even
    printf, as above, the string array name it blows.Even if I put zero.

    Any idea why?

    Thanks,

    Deaf

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you did not create array of strings - you created array of pointers to string
    Then you make each pointer point to the same variable name (which was incorectly use in the previos scanf)

    Code:
    /* this is string */
    char sample[] = "Something read from user and temporarly stored";
    /* this is array of 2 strings */
    char arr[2][100];
    /* and this how you copy string in C */
    if(strlen(sample) < 100)
    {
       strcpy(arr[0], sample);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2014
    Location
    Texas
    Posts
    6
    Ops.. I ment I need an array and each element is a string and not one character for each element.

    Each element of the array needs to be a string and not one character.

    Sorry about the confusion.

    Deaf
    Last edited by Deaf Smith; 07-11-2014 at 02:34 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart View Post
    you did not create array of strings - you created array of pointers to string
    It's actually an array of pointers to char, not pointers to string. Each of those pointers in the array is uninitialised, so accessing their values (including passing them to I/O functions) results in undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. G++ PGO Blows Up
    By User Name: in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2013, 06:11 PM
  2. Adding a string array to another string array?
    By vertigodown in forum C Programming
    Replies: 1
    Last Post: 04-20-2012, 09:56 AM
  3. Memory usage blows up
    By violatro in forum C Programming
    Replies: 4
    Last Post: 03-28-2012, 10:13 AM
  4. Replies: 1
    Last Post: 12-10-2008, 11:29 AM
  5. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM

Tags for this Thread