0

I am trying to compile and run in my desk an answers in one of the problems of leetcode, this 1 only for practice and learning proposes, but when i trying to run in a specific line of code there are the issue, with double pointers.

int **vale=(int**)malloc(n*sizeof(int*));
//*vale=(int*)malloc(m*sizeof(int*));
vale = spiralMatrix(m, n, Lista, ptr, vale);}

the problem is when the code reach the line of *returnColumnSizes the code crash. i know that a double pointer is like a matrix, and for use then we nned to allocate the memory, so is for that i am allocating the double pointer first. So now i can send it to the function but it doesn't work. what is that i doing wrong?

int** spiralMatrix(int m, int n, struct ListNode* head, int* returnSize, int** returnColumnSizes){

*returnSize = m;
*returnColumnSizes = (int*)malloc(m*sizeof(int)); ---> this crash
int **res = (int**)malloc(m*sizeof(int*));

thanks!

  • Welcome to Stack Overflow! [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Oct 21 '22 at 21:59
  • You should be passing the address of a variable that should be updated, not a double pointer. – Barmar Oct 21 '22 at 22:01
  • You should provide compilable example - I can't really make it out just from the snippets you have posted. – AnArrayOfFunctions Oct 22 '22 at 03:19
  • On terminology: `double *a` declares a "double pointer", `int *a` declares an "integer pointer", and `int **a` declares a "pointer to pointer to int" or "integer pointer pointer", but not a "double pointer". – William Pursell Oct 22 '22 at 05:03

1 Answers1

0

To avoid the XY Problem, I won't try to answer your LeetCode question. Instead, we will discuss your topic's central question, "How can I Correctly Pass this double pointer to a function in C".

Double pointer means simply an address that point to another pointer. For example, let's say we have a simple pointer, char* foo = "Hello", if we pass foo to a function, we are giving the address of "Hello", But what if we want to pass foo itself, here the concept of the double-pointer becomes handy, char** bar = &foo, now, if we pass bar, we are passing the address of foo and not "Hello" this time.

Full Example:

#include <stdio.h>

void change_by_pointer(char* s) {
    
    s = "One";
}

void change_by_double_pointer(char** s) {
    
    *s = "Double";
}

int main() {
    
    char* foo = "Hello";
    printf("foo = %s\n", foo);
    
    // Change by pointer
    change_by_pointer(foo);
    printf("foo = %s\n", foo);
    
    // Change by double pointer
    char** bar = &foo;
    change_by_double_pointer(bar); // Or change_by_double_pointer(&foo);
    printf("foo = %s\n", foo);

    return 0;
}

Output:

foo = Hello
foo = Hello
foo = Double

Finally, I'm not sure what you are trying to do with spiralMatrix() but based on the params datatypes, this is how you deal with it.

#include <stdio.h>

// Please ignore the behavior of this function.
// This is just an example of how you deal with double-pointers.
int** spiralMatrix(int m, int n, struct ListNode* head, int* returnSize, int** returnColumnSizes){

    *returnSize = m;
    
    *returnColumnSizes = malloc(m * sizeof(int));
    **returnColumnSizes = 3;
    
    int **res = returnColumnSizes;
    
    return res;
}

int main() {
    
    int returnSize = 1;
    int* returnColumnSizes = NULL;
    
    printf("returnSize = %d\n", returnSize);
    
    printf("Calling spiralMatrix()...\n");
    int** Res = spiralMatrix(2, 0, NULL, &returnSize, &returnColumnSizes);
    
    printf("returnSize = %d\n", returnSize);
    printf("returnColumnSizes = %d\n", *returnColumnSizes);
    printf("Res = %d\n", **Res);

    return 0;
}

Output:

returnSize = 1
Calling spiralMatrix()...
returnSize = 2
returnColumnSizes = 3
Res = 3
Hassan
  • 1
  • 4