Hi, I have this array of pointers:
char *test[50];
I want to be able to pass it to a function without the function being able to change anything in the array. Any ideas?
Printable View
Hi, I have this array of pointers:
char *test[50];
I want to be able to pass it to a function without the function being able to change anything in the array. Any ideas?
You probably want the function to make a copy of the array. Alternatively, you could write
Some compilers have been known to crash when passing large structures by value like this.Code:struct S {
int A[100];
};
void f(S s)
{
}
Why not using const?
Here's why.Quote:
Originally Posted by Micko
That and that fact that it doesn't really work:
Code:#include <stdio.h>
void bar( const int * const foo[] )
{
int x = 0;
/* This compiles and works as it should. */
for( x = 0; x < 5; x++ )
{
printf( "*foo[%d] is %d\n", x, *foo[x] );
}
for( x = 0; x < 5; x++ )
{
printf( "trying to change *foo[%d] to %d\n", x, x*x );
*foo[x] = x*x;
}
for( x = 0; x < 5; x++ )
{
printf( "*foo[%d] is %d\n", x, *foo[x] );
}
for( x = 0; x < 5; x++ )
{
printf( "trying to change foo[%c] to &x\n", x );
foo[x] = &x;
}
for( x = 0; x < 5; x++ )
{
printf( "*foo[%d] is %d\n", x, *foo[x] );
}
}
int main( void )
{
int a = 10, b = 20, c = 30, d = 40, e = 50;
int *foo[5] = { &a, &b, &c, &d, &e };
int x;
bar( foo );
for( x = 0; x < 5; x++ )
{
printf("*foo[%d] is %d\n", x, *foo[x] );
}
return 0;
}
[edit]
Here's the warnings if anyone cares:
:~/programming$ gcc -o constc constc.c -Wall
constc.c: In function `bar':
constc.c:16: warning: assignment of read-only location
constc.c:27: warning: assignment of read-only location
constc.c: In function `main':
constc.c:42: warning: passing arg 1 of `bar' from incompatible pointer type
[/edit]
*foo[0] is 10
*foo[1] is 20
*foo[2] is 30
*foo[3] is 40
*foo[4] is 50
trying to change *foo[0] to 0
trying to change *foo[1] to 1
trying to change *foo[2] to 4
trying to change *foo[3] to 9
trying to change *foo[4] to 16
*foo[0] is 0
*foo[1] is 1
*foo[2] is 4
*foo[3] is 9
*foo[4] is 16
trying to change foo[] to &x
trying to change foo[] to &x
trying to change foo[] to &x
trying to change foo[] to &x
trying to change foo[] to &x
*foo[0] is 0
*foo[1] is 1
*foo[2] is 2
*foo[3] is 3
*foo[4] is 4
*foo[0] is 5
*foo[1] is 1075140144
*foo[2] is 1075140144
*foo[3] is 1075140144
*foo[4] is 1075140144
Quzah.
When I try to compile your program I get comipler errors:
on lines:
And errors are:Code:....
for( x = 0; x < 5; x++ )
{
printf( "trying to change *foo[%d] to %d\n", x, x*x );
*foo[x] = x*x;//here
}
...
for( x = 0; x < 5; x++ )
{
printf( "trying to change foo[%c] to &x\n", x );
foo[x] = &x;//here
}
...
error C2166: l-value specifies const object
I've learned c on visual studio (first 6.0 then .net) so maybe that is the reason of errors. I always save my files as .c when I want strict C. Also when I try to compile this with Turo C 2.01 I get pretty much same errors something like Cannot modify const object in function....
Well GCC just gives warnings, not errors. I would prefer it give you errors, and not compile. But, as stated, GCC will compile it and allow it ro run.
Quzah.
IMO its not a defect of GCC that it only warns on that; sometimes one needs to compile old crusty code that takes certain liberties; and when you're writing your own code you should pay attention to all warnings anyway. I believe one can use -Werror or similar to make the all warnings errors too.