0

KEEP IN MIND I WROTE THIS CODE IN HERE TO SIMPLIFY MY QUESTION, PLEASE IGNORE MINOR MISTAKES IF THERE ARE ANY, MY PROBLEM FOCUSES ON MANIPULATING AN ARRAY OUTSIDE OF A FUNCTION BY USING IT'S POINTER.

I create a static array like this

char *array[size1][size2];

For the purposes of my example size1 = 3 and size2 = 6.

Then I use a function to pass the pointer of the array, and allocate strings to it.

int counter = 0;

void somefunction(char *a, char *b, int size1){
 while(counter<size1){
   //do some stuff to a
   strcpy(b+counter, a);
   counter++;
   printf("Adding %s to pos %i RESULT: %s\n",a,counter,b+counter);
 }

}

This works nicely and the output is correct (when outputting array b in the function), but it does not actually effect the array[] outside of the function. If I use

somefunction(a,array,size1);

The output will be correct, but the actual array outside of the function (array[]) will output gibberish random memory.

I output it like this:

for(int i=0;i<size1;i++){
    printf("%s\n",array[i]);
}

So what am i doing wrong here? I thought the array when passed to a function decays into a pointer to the first element (and therefore that array), but that doesn't seem to be the case. What am I doing wrong? How do I manipulate the array outside of the function by passing it's pointer?

Thanks!

10
  • Your array is 2D array and are you sure you are passing and reading correct dimension? I would pass &array[0][0] Commented Oct 28, 2016 at 11:46
  • @Swanad. &array[0][0] is a pointer to a pointer. Passing it to a function expecting a char * is invalid. Commented Oct 28, 2016 at 11:49
  • 1
    Do you want to have a list of strings or 2D array of strings? Commented Oct 28, 2016 at 11:51
  • Anyway, "decaying" of an array to a pointer only works as advertized on one dimensional arrays. If passing a n-dimensional array, the array name will "decay" into a pointer to an "n-1"-dimensional array. Commented Oct 28, 2016 at 11:52
  • 2
    It seems you are confusing a statically allocated string array - a 2D array of characters - with an array of pointers to any string - char* []. When you want an array of pointers to strings you would declare it as char* array [size]; but then you must allocate memory for the strings elsewhere. So what are you actually trying to do here...? Commented Oct 28, 2016 at 11:59

2 Answers 2

1

From the comments:

What Im trying to do is I calculate the length of the string (size2). So all of the strings contained will be of length 3 in this case, "ABC" for example. Size 1 is simply the amount of strings of size 3 that I need to store and iterate through. So size1=6 size2=3 means I want to have 6 strings accesable by ordered array, all of which are 3 chars in length. Once I compute size1 and size2 (I do this before creating the array) they do indeed stay constant.

In that case, you want to declare array as a 2D array of char:

char array[size1][size2]; // no *

Note that to store a string of length N, you need an N+1-element array to account for the 0 terminator. So if you're storing strings like "ABC" ({'A','B','C',0}), then size2 needs to be 4, not 3.

When you pass it to somefunction, the type of the expression array "decays" to "pointer to size2-element array of char" (char (*)[size2])1. Based on your description, it sounds like you declare array as a variable-length array (i.e., size1 and size2 are not known until runtime). If that's the case, then your function definition needs to look like

/**
 * size2 must be declared in the argument list before char (*b)[size2]
 * b may also be declared as char b[][size2]; it means the same thing
 */
void somefunction( char *a, size_t size2, char (*b)[size2], size_t size ) 
{
  ...
  strcpy( b[counter], a );
  ...
}

and should be called as

somefunction( a, size2, array, size1 );


  1. Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and the value of the expression will be the address of the first element of the array. In your case, N is size1 and T is "size2-element array of char".

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't mention this because I thought it wouldn't be relevant, but somefunction is actually a recursive function (for calculating permutations), so preferably I don't want to pass any static arguments... I might just go the lazy way and make a global array to simplify all of this. I will be back tomorrow and retry implementing your code, I'm too tired now. Thank you!
Thank you so much, took me a couple of days messing with strings and array, but now I fully understand it!
0

I believe you just need this:

char *array[SIZE];

Some function then looks like this:

int counter = 0;

void somefunction(char *a, char **b){
    while(counter < SIZE){
        //do some stuff to a
        *(b+counter) = calloc(sizeof(char), strlen(a)+1);
        strcpy(*(b+counter), a);
        counter++;
        printf("Adding %s to pos %i RESULT: %s\n",a,counter,b+counter);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.