1

Possible Duplicate:
gcc compile error: cast specifies array type

I want to check the difference in (int * ) and (int []). When I compile the following code, line one goes fine. But for line 2, my compiler gives the following error:

test.c:10: error: cast specifies array type

Can any one please tell me the meaning of this error and where have I erred?

#include<stdio.h>

void abc(int *a)
{
        int i;
        for(i=0;i<2;i++)
        {
            printf("%d",((int * )a)[i]);  //(1)
            printf("%d",((int [])a)[i]); //(2)
        }
}

int main()
{
    int b[2]={0,1};
    abc(b);
    return 0;
}
2
  • 1
    Does not exactly address why your cast fails, but stackoverflow.com/a/660812/567864 does a good job of explaining why an int* and int[] are not the same thing and how they differ internally to the compiler. Commented Apr 28, 2012 at 10:54
  • Who ever marked this question as duplicate, do you really think doing that would increase the health of SO? It's true other question gives the answer indirectly to this question but this question and answers here is more direct than the other question. Also, other question is irrelevant to the one OP asked here. Commented Jan 15, 2023 at 18:24

3 Answers 3

8

In general int * is a pointer (to an integer) and int[] is an array of unspecified size, which is a so called incomplete type. Incomplete types can only be used in declarations and must be completed in definitions. For example (the following code lies in global scope):

extern int[] p; //declaration of p

int p[5]; //definition of p - size must be specified

When you talk about function parameters, it's a whole other story. The declarations:

void f(int *p)

and

void f(int p[])

will be identical. It's just syntactic sugar if you will.


Edit: Other than that: If you are asking what's the difference between arrays and pointers: Everything! I'd link you to C-faqs.com for more concrete answers.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Anthales... I got my answer...:)
In that case it'd be nice to click that little accept button, so that others know that this question is solved. I also noticed you have another question with lots of answers but no accepted answer.
1

Try this

void abc(int *a){ 
  int i; 

  for(i=0;i<2;i++){
     printf("%d",*(a+i));  //(1)
     printf("%d", a[i]);   //(2) 
  } 
} 

You should spend more time understanding pointers and arrays. The name of array is a pointer to his first element, you can work with arrays using their names like with the memory piece and a pointer to it beginning

2 Comments

i know these things very well. But when we pass array in a function. we can keep formal parameter either of int *a or int a[] ? . why is so?
@Anthales gave you great answer above
0

Not at all. int * means a pointer to an integer in your memory. The [] bracket stands for an array. int a[10]; would make an array of 10 integers. int *a; would make a pointer to an integer.

Pointers aren't a good practice with simple types because they consume more memory.

For more answers check: http://www.cplusplus.com/doc/tutorial/arrays/ and http://www.cplusplus.com/doc/tutorial/pointers/.

5 Comments

Could you explain more memory usage when using pointers to simple types?
"they consume more memory" - you mean the extra 4 or 8 bytes per instance for the DWORD or QWORD that contains the address of the data? I'd say the fact that pointers make it easy to forget to clean them up (i.e. free() them) is a much bigger issue.
Yes well as Polynomial already said people forge to free them up and yes they consume extra 4 or 8 bytes. Why do I want extra 4 or 8 bytes if not necessary?
ya i agree with you, but when we pass array in a function. we can keep formal parameter either of int *a or int a[]. Can you elaborate this issue?
I think he meant that you shouldn't use create specific pointer for work with array of char[2] for example. Because pointer has 8 bytes size or 4 in different environment or compiler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.