# Weird warning in C array.

This topic is 5057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, In the following code:
#include<stdio.h>

void display_array(int *q[], int size);
int *q[]; /* Line 4 */

int main()
{
int a, b, c;
a = b = c = 20;
b++;
c = c + 60;
q[0] = &a;
q[1] = &b;
q[2] = &c;

display_array(q, 3);

printf("\n\nPress something to exit");
getchar();
return 0;
}

void display_array(int *q[], int size)
{
register int t;

for(t = 0; t < size; ++t)
{
printf("Address in q[%d]: %p.\nValue in q[%d]: %d.\n\n", t, q[t], t, *q[t]);
}
}


The compiler returns the message: "[Warning] array q' assumed to have one element", in line 4. What's the problem non declaring the index of an array? If I use a char array, it shows no message. I'm using DEVC++ with mingW.

##### Share on other sites
You need to specify the size (number of elements) of the array. For char arrays initialized like:
char str[]="blablabla";
The compiler can determine the size of the array automaticly.

##### Share on other sites
u declared q, but the size of the array is not mentioned. This type of declarings are done when the actual variable q is declared elsewhere in the project and u simply want to prototype it in some other C/CPP file. The program that u wrote is actually BUGGY ! , Rather thank the compiler to show some sortof warning message :)

##### Share on other sites
Maquiavel,

If you actually want the array q to be of a dynamic size (ie. you dont know up front until run-time how many elements you're going to haave) you're going to need to update your declaration as well as instantiate, and ultimately delete it. If you know there is always going to be x number of elements declare it as:

int q[ x ];

Below is the code to create, instantiate and delete an array of an unknown size.

// declaration of a dynamic array of ints// init. to 0 for checking later onint* q; (or int *q whichever one)q = 0;// instantiation, must be done prior to loding indiciesq = new int[ number of elements ];// don't forget to clean it up at the end of program execution// here's why I init. the pointer to 0 so if u never do inst.// it you won't get a fat runtime error when u try to delete itif( q )   delete [] q;

I think I got this correct but I'm kind of in a rush my lunch is wrapping up here....gotta get back to work and all that ^_^.

Good luck and if I'm incorrect in this someone feel free to correct me.

Permafried-

##### Share on other sites
Thanks Permafried,
But the code you gave is C++. Actually, I'm trying in C.
But, when I'll start learning C++, I'll see this topic again, to see this you've written.

Cya!

##### Share on other sites
Maquiavel,

If you're using raw C, instead of using new and delete lookup malloc() and free() in the MSDN. These two are the C equivalents of the new and delete keywords and works as follows:

// declaration of a dynamic array of ints// init. to 0 for checking later onint* q; (or int *q whichever one)q = 0;// instantiation, must be done prior to loding indiciesq = (int)malloc( ( num of elements * sizeof( int ) ) );// don't forget to clean it up at the end of program execution// here's why I init. the pointer to 0 so if u never do inst.// it you won't get a fat runtime error when u try to delete itif( q )   free( q );`

This should accomplish the same thing just slightly different syntax (I even made sure to keep the cast to a C-style cast since malloc() returns void* ^_^.)

Best of luck,

Permafried-

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999983
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!