-1
#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)

  • 2
    When you call `welcomeAndGetSize`, you are passing `stringArray` by value (as are all calls in C, so it is unchanged from NULL on exit. Even though you allocated memory for it inside. You need to change the function so that the function parameter is changed on the call and return. – bruceg Apr 27 '18 at 19:20
  • Consider trying to reduce your program to the smallest code which still demonstrates your problem before asking for help. Thus, you either solved your problem yourself (kudos!) or at least have a minimal sample for asking a good question. – Deduplicator Apr 27 '18 at 19:25
  • Aside: Consider the potential O(n*n) performance of `for (j = 0; j < strlen(temp); j++) { if ('\n' == temp[j]) { temp[j] = 0; } }`. To determine the result of `strlen(temp)` takes `n` operations and the function is called `n` times. A better similar code would use `for (j = 0; temp[j]; j++) { if ('\n' == temp[j]) { temp[j] = 0; } }` which is O(n). See [Removing trailing newline character from fgets()](https://stackoverflow.com/q/2693776/2410359) – chux - Reinstate Monica Apr 27 '18 at 20:05
  • the posted code does not compile!. amongst other things, it is missing the following statements: `#include ` and `#include ` – user3629249 Apr 28 '18 at 16:16
  • the function: `malloc()` expects its' parameter to be `size_t` not `int`. The returned type from `strlen()` is `size_t` not `int` so the code is comparing unsigned values to `signed` values. in function: `welcomeAndGetSize()` has the parameter `arr` but the body of the function does not use that parameter. These problems 'should' be corrected. – user3629249 Apr 28 '18 at 16:21
  • when calling any of the heap allocation functions, `malloc` `calloc` `realloc` 1) always check (!=NULL) the returned value to assure the operation was successful. 2) the returned type is `void*` which can be assigned to any pointer. Casting just clutters the code. Suggest removing the casting – user3629249 Apr 28 '18 at 16:23
  • regarding: `size = welcomeAndGetSize(stringArray);` this function should be modifying the variable: `char ** stringArray` however, the called funtion is only modifying the parameter (which is on the call stack) rather than the actual variable in `main()` – user3629249 Apr 28 '18 at 16:27
  • when compiling, always enable the warnings. then fix those warnings. ( for `gcc`, at a minimum use: `gcc -ggdb -Wall -Wextra -Wconversion -std=gnu11 -pedantic` the `-ggdb` is for the `gdb` debugger`, which is very helpful when debugging your code. – user3629249 Apr 28 '18 at 16:31

2 Answers2

2

I see multiple errors in this code. However, the initial issue causing your write access violation is related to your welcomeAndGetSize function. For a simpler example:

void funcB(int* value) {
    value = (int*)malloc(sizeof(int*));
}

void funcA() {
    int* number = NULL;
    funcB(number);
    // number is still NULL
}

At the end of funcA, number is still unset, and you've leaked some memory. Instead, you want to do something like:

void funcB(int** value) {
    *value = (int*)malloc(sizeof(int*));
}

void funcA() {
    int* number = NULL;
    funcB(&number);
    // number is allocated
}

You will need to adjust your code in a similar manner to allocate stringArray

In addition, the other answer is correct - instead of arr[i] = temp; you probably want strcpy(arr[i], temp);

retupmoca
  • 262
  • 1
  • 6
0
 arr[i] = (char*)malloc(sizeof(char) * strlen(temp) + 1);
 strcpy(arr[i],temp);

= does not copy the string

and in the main

stringArray = malloc(size * sizeof(char *));

btu before that you need to initialize somehow the size

0___________
  • 60,014
  • 4
  • 34
  • 74