ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
Close, but you want to use sizeof(int) instead of sizeof(int *). Yours will probably work accidentally though since an int is the same size as a pointer in most environments.
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:
Originally posted by itsme86 Close, but you want to use sizeof(int) instead of sizeof(int *). Yours will probably work accidentally though since an int is the same size as a pointer in most environments.
Ya... that was a typo on my part... hope it didn't cause any problems.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.