LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-11-2005, 05:15 PM   #1
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Conversion from char[100] to char* ?


I want to do something like this:
Code:
char[100] c;
char* s;
s=c;   // I get segmentation fault here
How do I do this? Thank you.
 
Old 12-11-2005, 06:50 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
When I do that it doesn't even compile.

I think if you put

char c[100];

it will work fine. (does for me)
 
Old 12-11-2005, 09:04 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Your problem lies in setting a string to a pointer to a string. Because s has no defined size, the program will seg fault. If you want to do this, s needs to be char s[100] as well.

You can do this in functions:

void copyString (char c[100], char *s)
{
strcpy (s, c);
}

As long as the length for s was predefined where this function is being called.

Hope this helps,
Mistro116
 
  


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
Help with Conversion from Sting to Char Diederick Programming 2 11-30-2005 08:00 AM
C programming - Char to Int Conversion ? indian Programming 4 09-30-2005 08:00 AM
C programming int to char conversion paranoid times Programming 15 03-17-2005 11:06 PM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
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 04:13 AM.

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