Sign in to follow this  
Maquiavel

Weird warning in C array.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 on
int* q; (or int *q whichever one)
q = 0;

// instantiation, must be done prior to loding indicies
q = 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 it
if( 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 this post


Link to post
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 this post


Link to post
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 on
int* q; (or int *q whichever one)
q = 0;

// instantiation, must be done prior to loding indicies
q = (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 it
if( 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-

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this