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?