LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-10-2011, 09:01 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
A question about pointers in C...


char *strerror(int errnum);

This function basically takes errno(int) and returns a pointer to a string describing the error.

So shouldn't this work?
Code:
char buf[32];
buf = strerror(13);
printf("error: %s\n", buf);
Yet gcc "returns incompatible types in assignment" for the 2nd line.

Isn't 'buf' by itself a char pointer? It's the same as *(buf+0) or &buf[0] no?
 
Old 07-10-2011, 09:15 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by trist007 View Post
Isn't 'buf' by itself a char pointer?
No, buf is declared as an array of char.

strerror() returns a char*; thus you should declare buf to be a char*. The area in memory that strerror() returns quite possibly is a statically declared buffer of N bytes. Thus the callee (that would be your program) should assign a pointer to that buffer. You cannot assign a char* to an array, although you can copy the string data using functions such as strcpy() or sprintf().

Anyhow, consider the following:
Code:
char array[30];

char* a = array;
Above, 'a' is pointing to the first byte (char) of the array. If you still have questions about the differences between pointers and arrays, I would suggest that you revisit your C reference book.

Last edited by dwhitney67; 07-10-2011 at 09:17 PM.
 
Old 07-11-2011, 01:10 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
It often helps to firmly remember
Code:
# type 'ptr-to-char'
char *p;

# type 'array-of-char'
char b[3];

# strictly speaking, there is no such thing as a 'string' type in C,
# instead it's a specially handled 'array-of-char' that needs '\0'
# as the last item to be usable by most 'string' fns

HTH
 
Old 07-11-2011, 10:20 PM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
great ty
 
  


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
question about pointers vijeesh_ep Programming 1 09-13-2004 03:13 PM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 05:14 AM
question on pointers h/w Programming 1 10-26-2003 02:54 PM
question on pointers to functions h/w Programming 3 10-06-2003 04:51 PM

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

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