LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-06-2010, 06:53 AM   #1
bhatia.ankur8
LQ Newbie
 
Registered: Jul 2010
Location: india
Posts: 13

Rep: Reputation: 0
Unhappy char pointer confusion


hey
i have a small problem related to char pointers.

i have to concatenate two strings. str1 and ".part".
but i do not no the size of str1.str1 is to be given by the user.
so when i use *str1 to store wat user entered and try to concatenate using strcat() i get error saying segmentation fault....

plz help
 
Old 07-06-2010, 07:03 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Without knowing which functions you're exactly using:

The user input is probably not a null terminated string, so you must make sure it's null terminated. Easiest is probably to zero the buffer before putting data in it; check man memset.

If you allocate a buffer for userinput, you must prevent the user to entering more data or limit whatever has been read. Check man fgets for reading userinput and limiting the amount of characters.

There is a dedicated programming section at LQ

PS Another point: if you declared a char *str1, this is only a pointer. You need to allocate memory before you can use it; a function that you use might already do that but without seeing the code it's difficult to say.

Last edited by Wim Sturkenboom; 07-06-2010 at 07:05 AM.
 
Old 07-07-2010, 02:06 AM   #3
bhatia.ankur8
LQ Newbie
 
Registered: Jul 2010
Location: india
Posts: 13

Original Poster
Rep: Reputation: 0
hey thnks a lot for a quick reply.......
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why is char* a string and not a pointer to a char? el_b Programming 2 09-25-2009 10:33 AM
C : Function to modify pointer to char introuble Programming 2 06-21-2006 12:24 PM
c char* pointer function question true_atlantis Programming 9 04-14-2006 01:29 PM
C pointers confusion - char ** = char [][] ?? saravkrish Programming 12 12-02-2004 10:06 AM
is *- -p = '\n' valid? can i set \n char to a pointer? feetyouwell Programming 1 10-01-2004 01:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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