C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2012, 03:12 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
Arrays initialization and declaration

why is this wrong to declare arrays and then initialize it
For instance

int x;
x =10;

the above code is valid.
but the below code raises an error
char searchstring[] = {"am not a fool"};
char tracks[10];
tracks ={"am not ready"};

Reply With Quote
  #2  
Old October 4th, 2012, 03:20 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 21 h 26 m 11 sec
Reputation Power: 1774
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.

Use instead
strcpy(tracks,"am not ready"); //!! needs 13 chars, not 10

Making sure of course that the string will fit.
Comments on this post
Technovicez agrees!
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old October 4th, 2012, 04:48 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
Thanks alot

Reply With Quote
  #4  
Old October 4th, 2012, 08:07 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
  #5  
Old October 5th, 2012, 12:26 PM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
I see, by experiment, that what you say is true. Isn't this a hole in the c language definition?

Not if it's been defined that way. The 'holes' in C are called undefined behavior.

char*a[] = {"am not a fool"};
a points to a pointer containing the string.
a is also an array of pointers.

char b[] = {"am not a fool"};
b points to the string.

printf("a %%p %p\n",a);
Display the pointer a points to.

printf("a[0] %%c %c\n",a[0]);
Display the 1st byte of the 1st pointer as a character.

printf("b[0] %%c %c\n",b[0]);
Display the first byte of the string as a character.

So what's the problem?

Reply With Quote
  #6  
Old October 5th, 2012, 12:48 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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.

Furthermore, as you say,
Quote:
char b[] =
b points to the string.

b is an array.
a string is a pointer.

I wouldn't write code like that, intentionally.

Reply With Quote
  #7  
Old October 5th, 2012, 02:49 PM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
  #8  
Old October 6th, 2012, 01:43 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m 21 sec
Reputation Power: 0
Mr.waltp AM a newbie to this language and i just started experimenting.

But still this point is not clear

par with my instance

i.e
int x;
x= 20;
/*if above code is correct */
then

char search[40] ;
search = {"am sid"};

/*why the above code is raising compilation erros*/

Please do explain me in terms of memory also.....

Reply With Quote
  #9  
Old October 6th, 2012, 01:52 AM
Technovicez Technovicez is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 27 Technovicez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old October 6th, 2012, 02:37 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
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.

I already showed you - use strcpy()

Reply With Quote
  #11  
Old October 6th, 2012, 04:48 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
To WaltP:
I recommend you run a pretty printer on c code with your favorite settings. Then the format of the code doesn't matter.

For instance, bcpp
which I originally learned as
cb

Dave.

Reply With Quote
  #12  
Old October 7th, 2012, 12:50 AM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
To WaltP:
I recommend you run a pretty printer on c code with your favorite settings.
Why?

Reply With Quote
  #13  
Old October 7th, 2012, 02:25 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,393 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 15 h 37 m 10 sec
Reputation Power: 383
With a pretty printer you'd transform your hate into love.

Reply With Quote
  #14  
Old October 7th, 2012, 02:51 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.
Comments on this post
Technovicez agrees: thankz a ton ,clear explanation

Reply With Quote
  #15  
Old October 7th, 2012, 04:33 PM
WaltP WaltP is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 19 WaltP User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
With a pretty printer you'd transform your hate into love.

As I said, I can live with it.

And if I couldn't, why use someone else's program? I'd just write my own that does exactly what I want...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Arrays initialization and declaration

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap