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;
}
Community
  • 1
  • 1
Abhishek Gupta
  • 1,173
  • 4
  • 12
  • 26
  • 1
    Does not exactly address why your cast fails, but http://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. – Corbin Apr 28 '12 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. – cipherdragon Jan 15 '23 at 18:24

3 Answers3

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.

Anthales
  • 1,158
  • 6
  • 10
  • Thanks Anthales... I got my answer...:) – Abhishek Gupta Apr 28 '12 at 11:01
  • 2
    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. – Anthales Apr 28 '12 at 11:12
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

vard
  • 2,142
  • 4
  • 30
  • 40
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/.

Majster
  • 3,611
  • 5
  • 38
  • 60
  • Could you explain more memory usage when using pointers to simple types? – patseb Apr 28 '12 at 10:51
  • 1
    *"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. – Polynomial Apr 28 '12 at 10:51
  • 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? – Majster Apr 28 '12 at 10:53
  • 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? – Abhishek Gupta Apr 28 '12 at 10:54
  • 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. – vard Apr 28 '12 at 10:55