Is it possible to assign a new value to an array name? - c

I read that;
Although an array name can be used as a pointer (after decaying into pointer), it's not possible to assign it a new value. Attempting to make it point elsewhere is an error:
while (*a != 0) // a is of (int *) type
a++; //wrong
On the other hand, when passed to a function, an array name is always treated as a pointer. The function call
largest = largest_num(b, n) // b is an array of int
for the function
int find_largest(int a[], int n)
{
....
....
}
causes a pointer to the first element of b to be assigned to a.
Above two statements ( in bold ) seems to me contradictory. I am confused.

In a function declaration, an array is treated as if you'd declared a pointer, so
int find_largest(int a[], int n) {
is processed as if it were
int find_largest(int *a, int n) {
So a is a pointer, not an array, and there's no contradiction.
Since it's a pointer, you can reassign a, e.g.
a++;
is allowed.

No contradiction there - you're still working with a pointer (to int), and int a[] notation is allowed only for convenience. Quoting the comp.lang.c FAQ:
Since arrays decay immediately into pointers, an array is never
actually passed to a function. You can pretend that a function
receives an array as a parameter, and illustrate it by declaring the
corresponding parameter as an array:
void f(char a[])
Interpreted literally, this declaration would have no use, so the
compiler turns around and pretends that you'd written a pointer
declaration, since that's what the function will in fact receive:
void f(char *a)
This conversion of array-like declarators into pointers holds
only within function formal parameter declarations, nowhere else. If
the conversion bothers you, you're under no compulsion to make use of
it; many programmers have concluded that the confusion it causes
outweighs the small advantage of having the declaration ``look like''
the call or the uses within the function.

Related

Why does a C array decay to a pointer when passed with size

I understand why an array decays to a pointer when passed to a function without specifying its size, eg.:
void test(int array[]);
Why does it do so when passed with the size? eg.
void test(int array[3]);
I am having trouble with sizeof under the latter function signature, which is frustrating as the array length is clearly known at compile time.
void test(int* array);
void test(int array[]);
void test(int array[3]);
All these variants are the same. C just lets you use alternative spellings but even the last variant explicitly annotated with an array size decays to a pointer to the first element.
That is, even with the last implementation you could call the function with an array of any size:
void test(char str[10]) { }
test("test"); // Works.
test("let's try something longer"); // Still works.
There is no magic solution, the most readable way to handle the problem is to either make a struct with the array + the size or simply pass the size as an additional parameter to the function.
LE: Please note that this conversion only applies to the first dimension of an array. When passed to a function, an int[3][3] gets converted to an int (*)[3], not int **.
Because you would not be able to call such a function properly. In almost all contexts an array decays to a pointer to the first element, and at any attempt to call the function an array would first be converted to a pointer and the call would be a mismatch. The only contexts where an array does not decay are the operators sizeof, _Alignof, _Alignas and &. These are easier to detect syntactically.

pass the location of a an array in a function

How can I pass the location of an array of pointers in a function in c i.e.
I have an int* array[10]; and through a function I want to assign the array[0] to be the location of a particular number say, 5. What should the argument of the function look like. I have tried:
void insert(int* array);
but it didn't work.
Arrays are always passed as a pointer pointing to the beginning of the array, even if you don't write it explicitly. So given this declaration of an array from your question:
int *array[10];
Both the following functions will accept a pointer to such an array:
int foo(int *array[]);
int foo(int **array);
They are just equivalent and I would suggest you use the second form, because it makes it explicit what type really is passed to the function.
inside the function, you can access any element, because the indexers are define the same way on a pointer to an array as on the array itself:
int foo(int **array)
{
int *sixthElement = array[5];
// [...]
}
Additional stylistic note: In C, it's common practice to attach the * when declaring a pointer to the identifier, not to the type, ie better write
int *array[10];
instead of
int* array[10];
This is because in a C declaration, there's no such thing as a pointer type, there's a pointer declarator (consisting of the *, optional qualifiers and the identifier) and this declarator declares a variable to be a pointer to the specified type. It's a subtle difference, but if you ever try to declare more than one variable in one line (don't do this, it's just for explanation here), you will notice it:
int *a, b; // <- declares a as a pointer to int, b just as an int

C function signature with asterisk

I have been attempting C recently and have been reading C source code,
however I came across this perculiar method signature with a * beside its name,
can someone explain this to me please
int *bubble_sort(int *numbers, int count, compare_cb cmp)
edit: I am referring to the * infront of bubble_sort
The asterisk has nothing to do with the function signature, it's just C's way of declaring pointers. The asterisk belongs to the type on the left, i.e. the return type for the function is int *, which is read out as "pointer to integer". The first argument has the exact same type, pointer to integer. You use this syntax everywhere in C, not just with function declarations.
The '*', used in this way, indicates that the 'bubble_sort()' function returns an address of (in other words, 'a pointer to) an integer, rather than an integer.
This concept is of pointers.
To know more about pointers see this link : http://www.tutorialspoint.com/cprogramming/c_pointers.htm
int *bubble_sort(int *numbers, int count, compare_cb cmp)
int *bubble_sort means that you are going to return the address of the integer,
and int *numbers means that you are getting the address of the variable as argument.
int a() is a function returning an int.
int * a() is a function returning a pointer to an int.
in your case the function returns a pointer to an integer.
guessing from the function name it is returning the sorted array.
an array in C is notated as a pointer to the first element:
int a[5];
int *b=a;
a and b are the same
However I guess you still have to do some more research on pointers which is not as complicated.
Perhaps this links might help: Pointer Primer

Correct C syntax for "address of" character pointer array?

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?
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.
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).
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);
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
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.
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.

Why is function(char * array[]) a valid function definition but not (char (*array)[] in C?

I think that it is because the former is an array of pointers to char and the latter is a pointer to an array of chars, and we need to properly specify the size of the object being pointed to for our function definition. In the former;
function(char * p_array[])
the size of the object being pointed to is already included (its a pointer to char), but the latter
function(char (*p_array)[])
needs the size of the array p_array points to as part of p_array's definition?
I'm at the stage where I've been thinking about this for too long and have just confused myself, someone please let me know if my reasoning is correct.
Both are valid in C but not C++. You would ordinarily be correct:
char *x[]; // array of pointers to char
char (*y)[]; // pointer to array of char
However, the arrays decay to pointers if they appear as function parameters. So they become:
char **x; // Changes to pointer to array of pointer to char
char (*y)[]; // No decay, since it's NOT an array, it's a pointer to an array
In an array type in C, one of the sizes is permitted to be unspecified. This must be the leftmost one (whoops, I said rightmost at first). So,
int valid_array[][5]; // Ok
int invalid_array[5][]; // Wrong
(You can chain them... but we seldom have reason to do so...)
int (*convoluted_array[][5])[][10];
There is a catch, and the catch is that an array type with [] in it is an incomplete type. You can pass around a pointer to an incomplete type but certain operations will not work, as they need a complete type. For example, this will not work:
void func(int (*x)[])
{
x[2][5] = 900; // Error
}
This is an error because in order to find the address of x[2], the compiler needs to know how big x[0] and x[1] are. But x[0] and x[1] have type int [] -- an incomplete type with no information about how big it is. This becomes clearer if you imagine what the "un-decayed" version of the type would be, which is int x[][] -- obviously invalid C. If you want to pass a two-dimensional array around in C, you have a few options:
Pass a one-dimensional array with a size parameter.
void func(int n, int x[])
{
x[2*n + 5] = 900;
}
Use an array of pointers to rows. This is somewhat clunky if you have genuine 2D data.
void func(int *x[])
{
x[2][5] = 900;
}
Use a fixed size.
void func(int x[][5])
{
x[2][5] = 900;
}
Use a variable length array (C99 only, so it probably doesn't work with Microsoft compilers).
// There's some funny syntax if you want 'x' before 'width'
void func(int n, int x[][n])
{
x[2][5] = 900;
}
This is a frequent problem area even for C veterans. Many languages lack intrinsic "out-of-the-box" support for real, variable size, multidimensional arrays (C++, Java, Python) although a few languages do have it (Common Lisp, Haskell, Fortran). You'll see a lot of code that uses arrays of arrays or that calculates array offsets manually.
NOTE:
The below answer was added when the Q was tagged C++, and it answers from a C++ perspective. With tagged changed to only C, both the mentioned samples are valid in C.
Yes, Your reasoning is correct.
If you try compiling the error given by compiler is:
parameter ‘p_array’ includes pointer to array of unknown bound ‘char []’
In C++ array sizes need to be fixed at compile time. C++ standard forbids Variable Lenght Array's(VLA) as well. Some compilers support that as an extension but that is non standard conforming.
Those two declarations are very different. In a function parameter declaration, a declarator of [] directly applied to the parameter name is completely equivalent to a *, so your first declaration is exactly the same in all respects as this:
function(char **p_array);
However, this does not apply recursively to parameter types. Your second parameter has type char (*)[], which is a pointer to an array of unknown size - it is a pointer to an incomplete type. You can happily declare variables with this type - the following is a valid variable declaration:
char (*p_array)[];
Just like a pointer to any other incomplete type, you cannot perform any pointer arithmetic on this variable (or your function parameter) - that's where you error arises. Note that the [] operator is specified as a[i] being identical to *(a+i), so that operator cannot be applied to your pointer. You can, of course, happily use it as a pointer, so this is valid:
void function(char (*p_array)[])
{
printf("p_array = %p\n", (void *)p_array);
}
This type is also compatible with a pointer to any other fixed-size array of char, so you can also do this:
void function(char (*p_array)[])
{
char (*p_a_10)[10] = p_array;
puts(*p_a_10);
}
...and even this:
void function(char (*p_array)[])
{
puts(*p_array);
}
(though there is precious little point in doing so: you might as well just declare the parameter with type char *).
Note that although *p_array is allowed, p_array[0] is not.
Because,
(1) function(char * p_array[])
is equivalent to char **p_array; i.e. a double pointer which is valid.
(2) function(char (*p_array)[])
You are right, that p_array is pointer to char array. But that needs to be of fixed size in the case when it appears as function argument. You need to provide the size and that will also become valid.

Resources