//prg #1 .. gives an error
void main()
{
int a[]={0,1,2,3,4,5};
int b[]=a; // Error ...why ?
for(int i=0;i%26lt;5;i++)
printf("%d", b[i]);
}
//prg #2 ...gives no error
void print(int b[]) // No Error ...why ?
{
for(int i=0;i%26lt;5;i++)
printf("%d", b[i]);
}
void main()
{
int a[]={0,1,2,3,4,5};
print(a);
}
[]\[] [] []\[] (())
Another challenging question regarding C,C++ ...?
The problem boils down to this:
The assignment operator "=" will copy atomic types (char, int, double, float, short, ...), or it will copy structures that you define (not many people know that, but it is very useful), but it will NOT copy arrays.
That's just the way it is.
Reply:b[] =a is invalid.
the reason is that in c++, c arrays name is basically a pointer. it points to the first element of the array. so you cannot assign a pointers address to an element. the correct way to do is
b=a;
since u are passing 'a' to the function 'print', its address is copied and sent to the b and not b[]
Reply:same rpoblem
Reply:declared a as array but assigning as a single charater if u want to copy array use this
for(i=0;i%26lt;5;i++)
b[i]=a[i];
song downloads
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment