Strange GCC compile error with C code
Well its not really an error but i keep getting a warning about missing braces
around my array. here is the code. Code:
some reason keep getting a warning about this kind of declration. i aslo get a warning about incompatible pointer types when trying to make an array constant in a function but not constant in the main program. thus with function void shuffle(const int *deck[]); |
What exactly are you trying to do with that declaration?
|
trying to intlize the double subscrpted array to 0's
acording to my book you can intilize them by only having to intilize the first element to 0 the compiler will intilize the rest of them. Note that the program works fine just getting the stupid warnings. warning: missing braces around initializer warning: (near initialization for `deck[0]') in this program and this one with the function issue i talked about earlier warning: passing arg 1 of `shuffle' from incompatible pointer type |
When you initialize a 2d array you use nested braces like:
int array[2][3] = { { 1, 2, 3 }, {4, 5, 6} }; So if you did: int deck[4][13] = { { 0 } }; it would get rid of the warning. |
ahh strange why my book wouldnt mention that.
I understand why im getting the other error its because the value im passing to the function is not const in the main program but of course that is the point. So my next question would be if i dont want the function to be able to modify the information in the array that i pass but beable to access its contents how would i be able to do this without generating an error normally i would ignore the error but i dont want gcc to compile it not as constant cause that would defeet the purpose. by the way is this a good idea for security of functions or is this just a supid step that is no longer used or needed with functions in c programminig book i have is quite old |
I'm going to jump back to the array initialize question real quick.... I'd use memset instead to initialize. Look at the man page to get the full description... but I think something like "memset(deck,0,4*13*sizeof(int *));" would work well for you.
As for the const problem... cast the array as const when you pass it into the function. |
Quote:
|
Non-const variables are automatically promoted to const when passed as arguments to functions expecting const types. No cast necessary and no warning should be generated. If's getting a warning it's due to some other mistake. And warnings regarding pointers are often not harmless as warnings can be.
|
If you have a statically allocated two-dimensional array that you want to pass to a function to alter the value of its elements, this example program demonstrates two variants:
Code:
#include <stdio.h> |
Quote:
|
Hivemind
the whole point of my issue was to have the functions NOT be able to modify the contents of the array. In order to follow the path of least privliage. i want the main to be able to modify the contents of the array and not the function. here is code. Code:
as you can see the function should not modify the array just print its values. |
Well, you're function in the OP was called shuffle which to me means something that can change the passed deck. But given my code it's trivial to see how to pass your twodimensional array around.
|
yea sorry i was looking at something called shuffle in my book and posting about the warning at the same time.
i normally dont make arrays static tho because they are passed by refrence to functions anyway. unless of course the array is declared in the function and i want to retain the values after the function exits. i however dont understand your use of static here. static as far as i have been told just means that the data will not be destoyed after a function exits. |
When I said statically allocated I simply mean allocated on the stack as opposed to allocated on the heap, which is done, as I'm sure you know, by calling malloc(). Anyway, if you just want to print the two-dimensional array you can pass it to a function like I'm doing in shuffle() or shuffle2(). shuffle2() is a bit more complicated but as a bonus you don't have to specify any boundaries when declaring it. Make the parameter const if it's not supposed to be altered (I always do that).
|
and thus is my issue with GCC
it complains when i make it const in the function and not in the main says im passing imcompatible pointer types ?? is this just a trivail warning that i should ignore. |
All times are GMT -5. The time now is 07:20 PM. |