LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-02-2010, 04:15 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
const char * question


Hi, its been a while since I have worked with C strings.

I have a function that needs to return a const char*. The char will be 5 charachers long (4 + NULL).

Code:
const char* getname( void )
{
   const char *name = (const char *)malloc(5);//(4+NULL)

   int newfilenumber = 5;

   //Format the string so it ahs leading 0's e.g. '0005'
    sprintf(filename,"%04d\n",newfilenumber);

    return name;

}
But I get the error: invalid conversion from ‘const char*’ to ‘char*’
on the sprintf line.

What am I doing wrong??

Thanks
 
Old 02-02-2010, 04:27 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Code of OP:
Code:
sprintf(filename,"%04d\n",newfilenumber);
Where is this variable 'filename' declared ?

Last edited by Aquarius_Girl; 02-02-2010 at 04:28 AM.
 
Old 02-02-2010, 04:46 AM   #3
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
Sorry a typo on my part.

the first line should be
[CODE]
const char *filename = (const char *)malloc(5);//(4+NULL)/[CODE]
 
Old 02-02-2010, 04:52 AM   #4
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Do you understand what const means in const char *filename? Do you know the difference between it and char *const filename? From `man sprintf` on my system:

Code:
int sprintf(char *str, const char *format, ...);

Last edited by carbonfiber; 02-02-2010 at 04:53 AM.
 
Old 02-02-2010, 04:55 AM   #5
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Original Poster
Rep: Reputation: 16
not really
 
Old 02-02-2010, 06:03 AM   #6
verdeboy2k
Member
 
Registered: Jan 2004
Location: /dev/random
Distribution: Gentoo amd64, CrunchBang amd64
Posts: 350

Rep: Reputation: 32
Basically, it cant be a constant if you expect sprintf to modify it, cast it afterwards.

This should work:

Code:
const char* getname( void )
{
   char *filename = (char *)malloc(5);//(4+NULL)

   int newfilenumber = 5;

   //Format the string so it ahs leading 0's e.g. '0005'
    sprintf(filename,"%04d\n",newfilenumber);

    return (const char *)filename;

}
 
Old 02-02-2010, 07:20 AM   #7
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by carbonfiber View Post
Do you understand what const means in const char *filename? Do you know the difference between it and char *const filename?
The const keyword means "constant" or in other words "read-only".

Though, if you define

Code:
const char *str;
Pointer to a constant/read-only character (or char array), though the memory where the pointer points to can't be modifed (at least NOT through this type)

Code:
char *str;
Pointer to a character (or char array), memory can be modified

Code:
char * const str;
Constant pointer to a character (or char array), meaning that the memory where the pointer points to can be modified, but the pointer itself can't be modified

And finally,

Code:
const char * const str;
Constant pointer to a constant/read-only character (or char array)

Hope that helps to understand the differences,

- Andi -
 
  


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
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 06:01 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
conversion from ‘const char*’ to ‘unsigned char*’ rubadub Programming 2 02-08-2008 05:45 PM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM

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

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