2

Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char *str = “…” and char str[N] = “…”?

I am a beginner in C programming. I am confused over these things:

char ar[]="hello";
char ar2[], *ar3;
ar2=ar;
ar3=ar;

Also is the above legal in case of 2D arrays like:

int arr[2][2]={{1,2},{3,4}};
int arr1[2][2],**arr2,*arr3;
arr1[0]=arr[0];
arr1=arr;
arr2=arr;
arr3=arr;

The other confusion was that I have seen above valid in case of structure.

struct test{
int ar[2];
} t1, t2;

int main()
{
t1.ar[0]=0;
t1.ar[1]=1;
t2.ar=t1.ar;
}

Please bear with me, I am a beginner in C.

Community
  • 1
  • 1
Gaurav
  • 1,005
  • 3
  • 14
  • 33

1 Answers1

2
char ar[] = "hello";
char ar2[];
char *ar3;

An array is a non-modifiable lvalue, so the following statement is invalid:

ar2 = ar;

Otherwise, it is possible to affect this value to a pointer.

ar3 = ar;

The behavior is the same with 2D arrays and structure members.

C11 (n1570)

§ 6.3.2.1 Lvalues, arrays, and function designators
A modifiable lvalue is an lvalue that does not have array type [...].

§ 6.5.16 Assignment operators
An assignment operator shall have a modifiable lvalue as its left operand.

Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93