Code:
first my english not that good so try to understand
so im trying to do a very simple task
i need to write a function that gets an empty array of strings and its size and initialize it (for exemple "names={"dani", "gabi", "bobi"}, names is array of size 3 and each one hold a string that the user puts)
so first the user need to put the size then i allocate the array and then i trying to get strings for the array each one for diffrent address
now for some reason its work greatly for the rest of the array exept for the one in the first index (arr[0])
i understand that because the "ENTER" is a char it gets it for the first string (when the user puts the size...)
but i dont want it to be like this and im trying to fix it for hours and im hopeless
please help me with that im geting crazy i might punch my comuter because of rage!!
Code:
#define _CRT_SECURE_NO_WARNINGS#define SIZE 21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { FAIL = 0, SUCCESS = 1 };
int init_arr(char ***arr, int *size);
int main()
{
int i, sizes = 0;
char **course = NULL;
init_arr(&course, &sizes);
for (i = 0; i< sizes; i++)
puts(course[i]);
free(course);
return 0;
}
int init_arr(char ***arr, int *size)
{
int i;
char* c;
printf("Enter the Number of--->:");
scanf("%d", size);
*arr = (char**)malloc((*size) * sizeof(char*));
if ((*arr) == NULL)
{
printf("Error");
return FAIL;
}
for (i = 0; i < (*size); i++)
{
c = (char*)calloc(SIZE, sizeof(char));
if (c == NULL)
{
printf("Error");
return FAIL;
}
printf("Enter Name %d-->:", i);
fgets(c, SIZE, stdin);
(*arr)[i] = c;
}
return SUCCESS;
}