#include <stdio.h>
#define TEMP_SIZE 50
void getUserInput(char ** arr, char * temp, int size);
int welcomeAndGetSize(char ** arr);
/*
This Code is taking from the user his number of friends and allocating a pointer to a pointer,in each index its inputes the name of his friends and then sorting the names
via alphabetical order
*/
int main(void)
{
int i = 0;
int size = 0;
char ** stringArray = 0;
char tempStore[TEMP_SIZE] = { 0 };
size = welcomeAndGetSize(stringArray);
getUserInput(stringArray, tempStore, size);
for (i = 0; i < size; i++)
{
printf("%s\n",stringArray[i]);
}
free(stringArray);
getchar();
return 0;
}
/*
This Functions welcomes the user and gets the number of friends from him.
input:the pointer to pointer array - char** arr.
output:the number of firends - int size.
*/
int welcomeAndGetSize(char ** arr)
{
int size = 0;
printf("Please Enter Number Of Friends: ");
scanf("%d",&size);
getchar();
arr = (char**)malloc(sizeof(char*) * size);
return size;
}
/*
This Function gets frome the user the name of his friend and dynamically allocates the memry needed for the string
and inputes it in the pointer to pointer.
input:the pointer to a pointer - char ** arr,a char array to temporarly store the string, the number of friends.
output:none.
*/
void getUserInput(char ** arr,char * temp, int size)
{
int i = 0;
int j = 0;
for (i = 0; i < size; i++)
{
printf("Please Enter The Name Of The %d friend: ",i + 1);
fgets(temp,TEMP_SIZE,stdin);
for (j = 0; j < strlen(temp); j++)
{
if ('\n' == temp[j])
{
temp[j] = 0;
}
}
arr[i] = (char*)malloc(sizeof(char) * strlen(temp));
arr[i] = temp;
}
}
This code is homework i get in class and while using pointer to a pointer i made a mistake,i know where the mistake is in the code, Its in Function getUserInput:
arr[i] = (char*)malloc(sizeof(char) * strlen(temp));
arr[i] = temp;
i get the following messege:
Exception thrown: write access violation. arr was 0x1110112. occurred Please explaine why i'm getting this error msessge. (p.s i'm using Visual Studio)