Discuss Arrays initialization and declaration in the C Programming forum on Dev Shed. Arrays initialization and declaration C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 3,393
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
I see, by experiment, that what you say is true. Isn't this a hole in the c language definition?
"abd" is a pointer.
{ } initialization is an array.
{"abc"} should be an array of pointers.
How c handles this depends on the declaration:
Code:
#if 0
display from program run:
a %p 0x601030
a[0] %c
b[0] %c a
#endif
#include<stdio.h>
char*a[] = {"am not a fool"};
char b[] = {"am not a fool"};
int main() {
printf("a %%p %p\n",a);
printf("a[0] %%c %c\n",a[0]);/* format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat] */
printf("b[0] %%c %c\n",b[0]);
return 0;
}
__________________
[code]Code tags[/code] are essential for python code!
Posts: 3,393
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
Clearly the behavior is defined, and I wasn't included on the c standards committee. My misunderstanding was on the right hand side, not on the left hand side.
In the initializer
{"am not a fool"}
The curly braces indicate an array. That's one level of indirection.
The "string" represents a pointer. That's a second level of indirection.
thus, while
char b[] = {"am not a fool"};
might be unambiguous and supported, it looks wrong.
Posts: 19
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Clearly the behavior is defined, and I wasn't included on the c standards committee. My misunderstanding was on the right hand side, not on the left hand side.
In the initializer
{"am not a fool"}
The curly braces indicate an array. That's one level of indirection.
They do? I thought they simply indicated a group. What's inside might be an array... Braces do not indicate an array.
Quote:
Originally Posted by b49P23TIvg
The "string" represents a pointer. That's a second level of indirection.
Yep. And nope. It's still just a string.
Quote:
Originally Posted by b49P23TIvg
thus, while
char b[] = {"am not a fool"};
might be unambiguous and supported, it looks wrong.
It's the same as
char b[] = "am not a fool";
What's the difference between:
Code:
if (i == 1) b = 25;
Code:
if (i == 1)
{
b = 25;
}
One has curly braces. It must be different according to your logic.
Quote:
Originally Posted by b49P23TIvg
Furthermore, as you say,
b is an array.
a string is a pointer.
I wouldn't write code like that, intentionally.
Fine. Then don't. You need to write code to your own style. Just understand that others may have a different style and you need to be able to distinguish correct and incorrect syntax in either style.
For example, my style for an IF is:
Code:
if (i == 1)
{
b = 25;
}
I hate this format:
Code:
if (i == 1) {
b = 25;
}
and despise this format:
Code:
if (i == 1)
{
b = 25;
}
All are acceptable conventions and I cannot tell the other programmers they are wrong. I just have to live with it. Under my breath, though...
Posts: 27
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
[QUOTE=salem]char searchstring[] = {"am not a fool"};
This is initialisation, which you can do for an array.
> char tracks[10];
> tracks ={"am not ready"};
This is assignment, which you can't do for arrays.
sir if we declare an array and take input from stdin then how itz able to assign our value to array .if its possible in that case then why itz not possible in the above code.
Posts: 3,840
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
Quote:
Originally Posted by Technovicez
sir if we declare an array and take input from stdin then how itz able to assign our value to array .if its possible in that case then why itz not possible in the above code.
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by Technovicez
int x;
x= 20;
/*if above code is correct */
then
char search[40] ;
search = {"am sid"};
/*why the above code is raising compilation erros*/
Because in the 1st code x is an int.
It can be assigned to, among other things.
On the other hand, in the 2nd code search is an array.
Arrays, in C, cannot be assigned to (they can be initialized).
Why can't arrays be assign to, you ask.
... Well ... arrays are, in C, second class citizens. They only exist as arrays in a few places (definition, arguments to sizeof, and arguments to unary & operator). In all other places, they get automatically converted to a pointer to their first element. So, the assignment in the 2nd code is (would be if it were legal) converted to
Code:
&search[0] = {"am sid"};
and, obviously, the address of the first letter in the string cannot be changed.