2

I need to pass into a function the address of a character pointer array, what would be the correct function declaration/prototype syntax for that?

I tried:

    void myFunc(char &*a[]);

But get an expected ; , or ) before & error.

I also tried:

    void myFunc(char **a);

Since the pointer I would be passing in is indeed a pointer to a pointer, but I get yet another error. This time the error has to do with: expected char **, but got char *[]

Or something like that. I have since attempted other solutions so I not remember exactly the error.

Any suggestions?

4
  • 3
    I'd suggest posting the error message and the bit of code that produced the error message. Commented Oct 1, 2013 at 18:32
  • Can you provide some more code, especially how you are attempting to call myFunc as it is stated now the question is not clear. Commented Oct 1, 2013 at 18:41
  • 1
    Based on what you've said, the second approach sounds like it should be correct. Try it again, and this time post the actual error message (not from memory this time) along with the line it's complaining about. You also might want to show the the code that's calling the function so we can verify that you're doing what you're describing. Commented Oct 1, 2013 at 18:45
  • Please provide the code that how you are calling myfunc() it would be easy for us to debug Commented Oct 1, 2013 at 19:19

6 Answers 6

4

Assuming you have an array declared as

char *a[N];

and you want to pass a pointer to the array, as in

foo( &a );

then the prototype for foo needs to be

void foo( char *(*aptr)[N] );

Note that in this case, the size of the array must be declared; a pointer to an N-element array is a different type from a pointer to an M-element array.

Normally, you don't want to do this; instead, you would normally just pass the array expression like so:

foo ( a );

and the corresponding prototype would be:

void foo ( char **aptr );

Except when it is the operand of thesizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer toT", and the value of the expression will be the address of the first element of the array.

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

3 Comments

What I was intending on doing was passing a pointer to the array of pointers and assigning each pointer in the original array to some block of memory with a string stored there.
Must the size of the array be declared in the function definition? Or can an array with defined length be passed in?
It is not necessarily to declare the size of the array; C will accept void foo(char *(*aptr)[]);. The type of aptr is then incomplete, but the C implementation does not need it as long as no expression requiring the size of *aptr is evaluated. The called function will need to know which elements in the array it should operate on (e.g., the first J elements).
2

Updated Answer For Modified Problem Statement

Given what you have said in comments, there is no need to pass a pointer to an array. You can simply pass a pointer to the first element of the array. Such a pointer suffices because the remaining elements of the array are obviously located after the first element.

To write a function that sets pointers in an array of pointers to char, do this:

void MyFunction(int NumberToSet, char *Pointers[])
{
    for (int i = 0; i < NumberToSet; ++i)
    {
        Pointers[i] = SomeString;
    }
}

In the above, SomeString must have type “pointer to char”. This could be a string, such as "Hello", or an array of char (which is automatically converted to a pointer to char), or some identifier x that has been declared as char *x (and has been initialized or assigned), for example.

To use this function, call it like this:

char *MyArrayOfPointers[SomeNumber];
MyFunction(NumberOfPointersIWantToSet, MyArrayOfPointers);

Original Answer

In most cases, to pass an array of pointers to char to a function, it suffices to pass the address of the first element. In this case, you would use either of these (they are equivalent):

void myFunc(char **a)
void myFunc(char *a[])

If you truly want to pass the address of the array, you would use:

void myFunc(char *(*a)[])

In this case, the type of a is incomplete, since the dimension is missing. Depending on what you intend to do with a, you may need to provide the dimension in the declaration.

When calling myFunc and passing it some array declared as char *array[N];, you would pass it, in the former case, as myFunc(array) and, in the latter case, as myFunc(&array).

7 Comments

I intended to pass a pointer to the array of pointers. Using such a pointer I was going to assign each pointer in the array to a block of memory containing a string.
I understand that char **a is a pointer to a char pointer. Is char *a[] a char pointer to an array? If so, it seems to follow naturally that char *(*a)[] is a pointer to a pointer to an array.
If you pass into a function myFunc(array), that would be a pointer to the first index of the array, or the pointer that IS at the first index of the array?
@sherrellbc: No, char *a[] is not a pointer to an array. It is, in a normal declaration, an array of pointers to char. In a function parameter declaration, it is adjusted to be char **.
@sherrellbc: In myFunc(array), array is converted to a pointer ot the first element. It is not converted to the first element; the value passed is not the pointer that is in the first element of the array.
|
1

try this as a function definition void myFunc(char *a[]) or void myFunc(char **a) then use it this way :

char *arr[20];
myFunc(arr);

Comments

1

Ok you are almost on the right path. void myFunc(char *a[]);

Example

void fun(char *a[]){
    printf("%s",*a);    //for accessing the next element do a+1
    }

int main(void) {
    char *x[3];
    x[0]="abcd";
    fun(x);   // here you are passing the address first array element 
    return 0;

DEMO

Comments

1

Declaration

void myFunc(char &*a[]);  

is not a valid C syntax.
To pass the address of character pointer arrays, use this instead

void myFunc(char *(*a)[]);   

*(*a)[] in the above function declares a as pointer to array of pointers to chars. Must note that a has an incompatible type. A suffix is needed in [] to make it complete.

10 Comments

void myFunc(char &*a[]); is it possible in c++, I ran into a similar issue before when I was trying to do that
@aboody; In C++ it is possible to call by reference but not in C.
After downvoting please leave a comment. It should be appreciated.
that wasnt me, however take at the error message I got here ideone.com/FkK2ed
In the grammar of the C standard, *(*a)[] is a declarator which is parsed as a pointer * and a direct-declarator (*a)[]. The latter is parsed as a direct-declarator (*a) and []. Therefore * binds to a first (so a is a pointer), then it binds to [] (so it is a pointer to an array), then it binds to the outer * (so it is a pointer to an array of pointers). You can also check cdecl.
|
-1

First of all, C is in general a "pass by reference" language. Some data items such as integers, floats, and single characters can be passed by value. But, arrays of those same data types are ALWAYS passed by reference.

Thus, when you say "I need to pass into a function the address of a character pointer array" then simply declare an array of character pointers in your function prototype as:

void myFunc(char *a[]);

Thus, char * declares a char pointer and a[] defines an array of them. To check this declaration refer to: http://www.cdecl.org/ which parses this expression as a "declare a as array of pointer to char".

The technical point is that the * binds with char rather than with a[].

So, C will pass a pointer to the data structure that you have declared. A discussion on this topic could delve into double pointers but for this question such a discussion is probably off topic.

4 Comments

Your first paragraph is completely wrong. C is a pass by value and doesn't have references. Pointers are passed by value.
self, but the pointer refers to data that is to be modified for the caller. It is NOT possible to modify the value of the passed ptr, hence it is passed by value, a value that references what is of actual interest.
Then clarify what you meant when you used word reference. Currently it is wrong.
self, I think that the statement "C is in general a "pass by reference" language" is sufficient.

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.