LinuxQuestions.org
Help answer threads with 0 replies.
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 12-13-2014, 12:58 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

Nothing. On the other hand, if the file-name were $(rm -rf /) (which is unusual but legal filename), the results would be different.
 
Old 12-13-2014, 02:09 PM   #17
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by stf92 View Post
So
Code:
char *a[10]="John";
is the only instance where I can do that? That is, during the array declaration?
You have an array of 10 pointers to char. You can assign them at anytime. However,that isn't a valid initializer.
Code:
char *a[10];
a[0] = "John";
As I pointed out elsewhere the memory pointed to in that instance is read only memory. If you wanted to strings that are mutatable you need to allocate memory and copy the string.

Also:
Code:
char* a[10] = { "John" };
 
Old 12-13-2014, 03:47 PM   #18
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by stf92 View Post
So
Code:
char *a[10]="John";
is the only instance where I can do that? That is, during the array declaration?
This is an invalid initialization.

You have declared an array of 10 pointers to characters... but tried to set a pointer - but did not specify WHICH pointer you want.

The usual way for something like this is:
Code:
char *a[10] = {"Bob"};
And the first pointer (a[0]) will be pointing to the constant array "Bob".
 
Old 12-13-2014, 04:15 PM   #19
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 stf92 View Post
So
Code:
char *a[10]="John";
is the only instance where I can do that? That is, during the array declaration?
I think you probably meant:

Code:
char a[10]="John";
That is one of the situations in which the C language itself (rather than just some standard functions) pretends to support strings.

That does work only as an initialization during array definition ("definition" is approximately what you meant by "declaration". That line of code does declaration, definition and initialization. But in C you can declare without defining or initializing and you can declare and define without initializing. So sometimes you need to be careful in a language features discussion to say exactly which of those you mean).

Anyway, that assignment of a string value to a char array is only valid as an initialization, not as a run time assignment.

Last edited by johnsfine; 12-13-2014 at 04:23 PM.
 
Old 12-13-2014, 04:43 PM   #20
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Yes, the asterisk is a typo. Thanks for clearing my doubts.
 
Old 12-13-2014, 06:14 PM   #21
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
char a[10]="John";

will be, in memory:
[J],[o],[h],[n],[0],[0],[0],[0],[0],[0]

you only need the string and a 0 to terminate it
char a[5]="John";

or just
char a[]="John";
 
  


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
Bash - passing associative arrays as arguments Lucien Lachance Programming 12 08-26-2013 12:43 PM
Passing variable arguments to ctypes function of C++ prasanthhs Programming 1 08-09-2012 08:30 PM
Problem:Passing arguments to thread function in C Thodoris21 Programming 24 06-07-2012 06:08 AM
Passing bidimensional arrays to function in C pittopitto Programming 5 05-18-2006 09:31 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM

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

All times are GMT -5. The time now is 05:21 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