-1

First of all, my english isn't good enough, so forgive my mistakes. I need to save/store student's names using an statical array. I'm using a bidimensional array, rows represent the student's class, and columns represts the student's name.

I also used malloc in order to store data inside Store_Student_Name().

So i wrote this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char chrStudent[50][9];

void Store_Student_Name(int j, int k)  
{   
    char chrName[100];
    char *p_aux = (char*)malloc(strlen(chrName) + 1); 
    fgets(chrName, 100, stdin);
    strncpy(p_aux, chrName, strlen(chrName)+1);
    chrStudent[j][k]= p_aux;
}    


int main()
{   
    printf("Class 0, Student 1 Name: ");
    Store_Student_Name(0,0);
    printf("\n0,0 %s\n", chrStudent[0][0]);

    printf("Class 0, Student 2, Name: ");
    Store_Student_Name(0,1);
    printf("\n0,1 %s\n", chrStudent[0][1]);

    printf("Class 0, Student 3, Name: ");
    Store_Student_Name(0,2);
    printf("\n0,2 %s\n", chrStudent[0][2]);

    printf("Class 0, Student 4, Name: ");
    Store_Student_Name(0,3);
    printf("0,3 %s\n", chrStudent[0][3]);

    system("PAUSE");
    return 0;
}   

This program is runing perfectly if i write short names for example "Pedro", but if i write "Pedro Luis Samaniego Peralta" this program crashes.

What am i doing wrong?

sarath
  • 513
  • 6
  • 18
Helio
  • 1
  • 1
  • Looks like the problem is with whitespace. What if you type something like PedroLuisDeLasMercedesAcosta? Does it work? – Fiddling Bits May 01 '14 at 13:56
  • @Fiddling Bits It does not work either. – Helio May 01 '14 at 13:59
  • `char chrStudent[50][9];` --> `char *chrStudent[50][9];` and `char *p_aux = (char*)malloc(strlen(chrName) + 1);` after `fgets`. – BLUEPIXY May 01 '14 at 14:01
  • Thank you. @BLUEPIXY that actually worked. I don't know why should i use char *p_aux = (char*)malloc(strlen(chrName) + 1); after gets. – Helio May 01 '14 at 14:16
  • Size to ensure dynamically not determined and only after the input. – BLUEPIXY May 01 '14 at 14:21
  • You should really switch all compiler warnings on and fix your code until none of them appears anymore. This `chrStudent[j][k]= p_aux;` would make the compiler issue a warning. – alk May 01 '14 at 14:37

4 Answers4

0

A row from char chrStudent[50][9]; can only have 8 characters plus 1 for the string terminator.

Enlarge it to taste; e.g. char chrStudent[50][/*bigger number here, say N*/];

(Moving on, you might want to change your strncpy to have a maximum value of N - 1.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Well, char type represents single character, not string. You need 3 dimentional array to store that data, because one dimension will be used to store string. You can do it by simply creating two-dimensional table that containt not characters, but pointers to arrays of chars (strings): char* chrStudent[50][9];

Then assignment chrStudent[j][k]= p_aux; will be correct.

user3581454
  • 101
  • 6
0

The line:

char chrStudent[50][9];  //name can be only 8 char long

As written, will hold 50 names 8 characters long or less. (8 characters and 1 null terminator)

If you need more than 8 characters, the second index has to be bigger:

char chrStudent[50][100];  //100 for example will provide for a name 99 char long.  

Also, because chrName has not yet been assigned a value at the time this function is called:

char *p_aux = (char*)malloc(strlen(chrName) + 1);  

There is no guarantee how much space p_aux will have.
Change line to:

char *p_aux = malloc(100);  //to match the value of the second index of `chrStudent[50][100]`.  

note: Because A void pointer in C can be assigned to any pointer without an explicit cast, you do not need to cast the return of malloc()
discussion on this point

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

try this

char *chrStudent[50][9];

void Store_Student_Name(int j, int k)  
{
    char chrName[100], *p_aux;
    int len;
    fgets(chrName, sizeof(chrName), stdin);
    len = strlen(chrName);
    if(len && chrName[len-1]=='\n')
        chrName[--len] = '\0';
    p_aux = malloc(len + 1); 
    strncpy(p_aux, chrName, len+1);
    chrStudent[j][k]= p_aux;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • That is because, last character may be an space and i should change to the null terminator. Thank you again. – Helio May 01 '14 at 14:28