LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Strange GCC compile error with C code (https://www.linuxquestions.org/questions/programming-9/strange-gcc-compile-error-with-c-code-355895/)

exvor 08-22-2005 02:18 PM

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:




 int deck[4][13] = {0};

im doing an exercize and trying to create a card shuffling program but for
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[]);

jtshaw 08-22-2005 04:09 PM

What exactly are you trying to do with that declaration?

exvor 08-22-2005 04:29 PM

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

itsme86 08-22-2005 05:36 PM

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.

exvor 08-22-2005 06:24 PM

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

jtshaw 08-23-2005 08:49 AM

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.

itsme86 08-23-2005 09:37 AM

Quote:

memset(deck,0,4*13*sizeof(int *));
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.

Hivemind 08-23-2005 10:06 AM

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.

Hivemind 08-23-2005 10:56 AM

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>

typedef unsigned short ushort;

static void shuffle(int [][13]);
static void shuffle2(int *, const ushort, const ushort);
static int* get_offset_from_index(int *, const ushort, const ushort, const ushort);

int
main(void)
{
  int deck[4][13] = {{0}};

  shuffle(deck);

  printf("%i\n", deck[0][0]);

  shuffle2(deck[0], 13, 4);

  printf("%i\n", deck[3][12]);
  printf("%i\n", deck[2][9]);

  return 0;
}

static void
shuffle(int deck[][13])
{
  deck[0][0] = 1;
}

static void
shuffle2(int *deck, const ushort rows, const ushort columns)
{
  *(get_offset_from_index(deck, rows, rows - 1, columns -1)) = 4711;
  *(get_offset_from_index(deck, rows, 9, 2)) = 256;
}

static int*
get_offset_from_index(int *ptr, const ushort max_x, const ushort x, const ushort y)
{
  int offset = max_x * y + x;
  return ptr + offset;
}


jtshaw 08-23-2005 12:28 PM

Quote:

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.

exvor 08-23-2005 02:36 PM

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:


#include<stdio.h>

void print_arry(const int *arry1[])

int main()
{

    int arry0[4][13] = {{0}};
    int count, coun;

  for (count = 1; count <= 4; count++)
        for (coun = 1; coun <= 13; coun++)
            arry0[count - 1][coun - 1] = coun;

  print_arry(arry0);


 return 0;
}

void print_arry(const int *arry1[])
{
    int row, column;
   
    for (row = 1; row <= 4; row++)
      for (column = 1; column <= 13; column++)
          printf("%d%c",arry1[row -1][column -1], column == 13 ? '\n' : ' ');

}


as you can see the function should not modify the array just print its values.

Hivemind 08-23-2005 04:12 PM

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.

exvor 08-23-2005 05:24 PM

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.

Hivemind 08-23-2005 05:29 PM

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

exvor 08-23-2005 05:39 PM

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.