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 01-26-2005, 07:24 AM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
what the... C code problem


Hi all,

I clearly am suffering from a fundemental lack of understanding. What is wrong with the following code snippet?

Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <unistd.h>

int main( int argc, char **argv) {

        char *a;
        int b = 100;

        a = ( char * ) malloc( 4 );
        strcpy( a, ( char * ) b );
        printf( "%s\n", a );
        return ( 0 );
}
I get a segmentation fault. Do I not have to malloc memory for the point to char? Is four bytes not enough? Do I not have to cast b as a pointer to char in order to strcpy it into a? Or am I approaching storing/concatenating an int into a string in the wrong way?

TIA.
 
Old 01-26-2005, 07:35 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
strcpy( a, ( char * ) b );

This isn't doing what you think it's doing. Its attempting to access memory location 100 and copy that to a. Which is why you have the segmentation fault.

sscanf( a, "%d", b );
 
Old 01-26-2005, 07:59 AM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi,

Thanks. You were right--that wasn't doing at all what I thought it was.
When I try sscanf, I get a warning:

junk.c:15: warning: format argument is not a pointer (arg 3)

And then the printf prints just the newline.

Ideas?

Thanks again.
 
Old 01-26-2005, 09:19 AM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I think you need this:

Code:
a=(char *)malloc(4);
sprintf(a,"%d",b);
...
 
Old 01-26-2005, 09:41 AM   #5
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Yep Matir is right mixing up my scan and my print.

Last edited by leonscape; 01-26-2005 at 02:07 PM.
 
Old 01-26-2005, 02:00 PM   #6
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Thank you both, all fixed now and ready to roll for a while!

Thanks again!
 
Old 01-26-2005, 02:08 PM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
No problem. Feel free to click the thanks button by my post... not that I know what it does, lol.
 
  


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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
C Code problem ar1 Programming 3 05-09-2005 07:04 AM
What is the problem with the below C++ code? coolguy_iiit Programming 4 01-24-2005 12:28 PM
Problem with Code eggoz Linux - General 6 04-09-2003 09:35 PM

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

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