0

Lacking money ATM so I'm offering $0.25 via paypal to the first person to point out what I did wrong in this code snippet -- I hope this doesn't violate the site rules or insult anybody.

I want to modify an multi-dimensional array in a function. It gets modified while in the function, but when scope returns to the main function the array is unchanged.

The function headers cannot be modified. Thanks for helping me out.

void getAlignment(char*s1, char*s2, char*s3, char*aligned[])
{
    /***********************
    Code here which assigns
    char**tmp to "different" "words"
    ***********************/

printf("tmp in getAlignment function\n");
printf("%s %s\n", tmp[0], tmp[1]); // prints "different words", as expected
    aligned = tmp;
}

int main(void)
{
    // skipped some code

    char** aligned = (char**)malloc(sizeof(char*)*2);
    aligned[0] = "should";
    aligned[1] = "change";

    printf("%s %s\n", aligned[0], aligned[1]); // prints "should change", as expected
    getAlignment(s1, s2, transcript, aligned); // how do i change aligned during this call?
    printf("%s %s\n", aligned[0], aligned[1]); // prints "should change"

   return 0;
}
Rob
  • 15
  • 3
  • what you want to do in getAlignment() function? What is the expected output? – user207064 Apr 16 '14 at 04:16
  • The idea is i have `aligned` in `main`, and I want to change it in `getAlignment` by having it point to stuff which the `getAlignment` method changed. – Rob Apr 16 '14 at 04:19

2 Answers2

1

When you write inside getAlignment:

aligned = (char**) malloc(2*sizeof(char*));

you are making the pointer GetAlignment::aligned point to some new memory. It no longer points to the memory that main::aligned points to. When you operate on this new memory, it has no effect on the memory that main::aligned was and is pointing to.

(Note - the :: is not C syntax, but my meaning is to disambiguate your two variables which are both called aligned in their local scope, despite the fact that they are two separate variables).

If your intent is that the code in getAlignmentmodifies the memory being pointed to by main::aligned then just remove the above line.

If your intent is that getAlignment be able to allocate new memory, and main::aligned be switched to use that new memory, then you have to pass main::aligned by reference (i.e. add an extra level of indirection in the function call). And don't forget to free() the previously-allocated memory.

BTW don't cast malloc.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • this "pointed" me in the right direction (bu dum pshhh). please let me know your paypal in case you'd like small compensation for your help. thanks very much. – Rob Apr 16 '14 at 05:01
  • Idon't use paypal... it's evil. happy to help for free anyway – M.M Apr 16 '14 at 05:06
0

You don't need to allocate 2D array again by aligned = (char**) malloc(2*sizeof(char*)); in function getAlignment because you already allocated it in main.

You require allocate each element of array like

int alignedStrLen = strlen(s3);
aligned[0] = malloc((alignedStrLen+1)*sizeof(char));
aligned[0][alignedStrLen] = '\0';
aligned[1] = malloc((alignedStrLen+1)*sizeof(char));
aligned[1][alignedStrLen] = '\0';

And and you should free the memory allocated by malloc.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • let's say in `main` i just declared `char**aligned` w/out initializing it. The idea is i want the function `getAlignment` to modify `aligned` regardless of what `aligned` points to. – Rob Apr 16 '14 at 04:39