LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-22-2005, 02:18 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
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[]);
 
Old 08-22-2005, 04:09 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
What exactly are you trying to do with that declaration?
 
Old 08-22-2005, 04:29 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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
 
Old 08-22-2005, 05:36 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 08-22-2005, 06:24 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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
 
Old 08-23-2005, 08:49 AM   #6
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 08-23-2005, 09:37 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 08-23-2005, 10:06 AM   #8
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 08-23-2005, 10:56 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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;
}
 
Old 08-23-2005, 12:28 PM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 08-23-2005, 02:36 PM   #11
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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.
 
Old 08-23-2005, 04:12 PM   #12
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.
 
Old 08-23-2005, 05:24 PM   #13
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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.
 
Old 08-23-2005, 05:29 PM   #14
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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).
 
Old 08-23-2005, 05:39 PM   #15
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Original Poster
Rep: Reputation: 66
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can gcc cross-compile C code to M68K Assembly? lowpro2k3 Programming 1 03-25-2005 07:50 AM
Strange error in C code zaichik Programming 2 02-08-2005 05:21 PM
Why this GTK example code compile error? searun Programming 1 07-22-2004 08:21 AM
why gcc permit this code to compile? is there a reason? zeppelin Programming 10 04-30-2004 04:54 AM
Strange Compilations Error with gcc 3.3.2 gamehack Debian 1 01-19-2004 09:19 AM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration