LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-26-2010, 06:23 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
pointer vs array declaration?


Whats the practical diffrens between these declarations in c?

char *a_str = "hello";

char a_str[] = "hello";
 
Old 07-26-2010, 06:50 AM   #2
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
You should get an error from:
Code:
char *a_str = "hello";
because that is assigning a pointer value of type (const char *) to a variable defined to have type (char *). The compiler MAY choose to create a private instance of the string literal "hello" just for this. If you had
Code:
const char *a_str = "hello";
const char *b_str = "hello";
you could (probably should) get the exact same pointer value stored in a_str and b_str. However with
Code:
char *a_str = "hello";
char *b_str = "hello";
if it doesn't flag that as a pointer type assignment/initialization error, it should give unique pointer values to each.

So, what if you mean to use const in your definition. Then, what is the practical difference? One makes a pointer object and arranges for it to point to a string literal somewhere. The other makes string space and initializes it. The practicality depends on your usage.

If you need a pointer that at different times may point to different strings, but needs to initially point some first string, the first example (with const) would do well. You can modify the pointer to point to other strings, later.

If you need string space that can be modified in place, but not increased in size, the second example would do well. You would get just enough space to hold the initial value. You may get more, but you cannot depend on that.
 
Old 07-26-2010, 06:59 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Thanks for the clearification.
 
Old 07-26-2010, 07:12 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Skaperen View Post
You should get an error from:
Code:
char *a_str = "hello";
I thought that specific abuse was grandfathered in from before it was considered incorrect, so normal compiler settings would not give you an error.

That "hello" ought to be const. It may cause a seg fault or worse if you actually write to it. But the compiler lets you assign its address to a pointer to non const.

Quote:
The compiler MAY choose to create a private instance of the string literal "hello" just for this.
"MAY" is hard to argue against. But I don't think the compiler does that.

Quote:
if it doesn't flag that as a pointer type assignment/initialization error, it should give unique pointer values to each.
I don't think so.

Quote:
One makes a pointer object and arranges for it to point to a string literal somewhere. The other makes string space and initializes it.
That is the answer to the question the OP asked.

Additionally, the first makes a non const pointer to a potentially shared, potentially read only literal. So writing through that pointer would be a run time error. The second makes a non shared writable copy of the text, so writing through that pointer (within range) is fine.

Unfortunately many C functions are declared and used in a way that prevents the compiler from knowing whether a char* will be written through. So the programmer must use char* for many pointers that logically ought to be char const* and pass those pointers to functions that from the compiler's viewpoint might be writing through the pointers. It is the programmer's responsibility then to track the way those functions are used to know that these pointers won't actually get written through.
 
1 members found this post helpful.
Old 07-26-2010, 09:43 AM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by johnsfine View Post
"MAY" is hard to argue against. But I don't think the compiler does that.
That's why I used MAY and emphasized it. A compiler trying to be safe about a grandfathered scenario might WANT to do that. I don't know that any do this.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Array declaration in C vkmgeek Programming 6 05-15-2008 03:01 PM
Array declaration? kalleanka Programming 2 11-19-2007 06:27 AM
mysterious pointer declaration irfanhab Programming 6 02-16-2006 11:07 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Is there a default size limit to a dictionary, list or array declaration ? Linh Programming 2 06-08-2004 11:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration