?
Solved

Adding a string to the end of a pointer using strcat()

Posted on 2003-03-13
12
Medium Priority
?
255 Views
Last Modified: 2010-04-15
I am currently trying to create a program as part of my Degree programme.

I am required to create a program to mimic the "ls -alR" command.

I can read in the directory and display it correctly, but controlling the directory currently being in is done through a string which is passed as a parameter to a function, ie  recurse(char *currpath)

I am then within the code trying to add a "/" to the end using strcat(currpath, "/");

Unfortunately, here is where I am getting a 'Segmentation fault'.

Any help with this owuld be appreciated.

-Tefal
0
Comment
Question by:Tefal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8127884
ur strcat is giving segfault because it doesnt have enough of space to hold more characters..
do this

say
char *str;
str=(char*)malloc(strlen("/home")+1);
strcpy(str,"/home");

so str is having "/home"
to add '/' do like this......
str=(char*)realloc(str,strlen(str)+2);
sprintf(str,"%s%c",str,'/');

printf("%s\n",str);
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8127887
>> Unfortunately, here is where I am getting a 'Segmentation fault'.

This means that strcat is overstepping the array's bounds.

>> strcat(currpath, "/");

Try the following instead,

char* cpath = (char*)malloc( sizeof(char) * strlen(currpath) + 2 );

sprintf( cpath, "%s%s", currpath, "/" );

Exceter
0
 
LVL 1

Expert Comment

by:nrohan
ID: 8127890
who has allocated memory to currpath and how much?
problem seems here that, when you append "/" to currpath, it overwrites some other memory.
try allocating 1/2 extra bytes to currpath.

if u post some code that will help.

rohan
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 8127892
Sorry, akshayxx. You posted while I ws typing. :-)

Exceter
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8127908
btw if u need to just list the directory .. then u might want to have a look on this
http://nodevice.com/sections/ManIndex/man1383.html
look at the example given in the manual page
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8127935
for Exceter:
         :)
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8127957
What does the recurse(char *currpath) do ?
Does it call itself again  recursivelly ?
Then maybe your stack is not enough.

Bad guess :)
Complete code will be easier to shoot.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8128002
Exceter :
our posts were pretty close ... i wud like to see the time difference to the last nano-second :P
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8128216
>> our posts were pretty close ... i wud like to see the time difference to the last nano-second :P

Yeah, that would be pretty interesting. :-)

Exceter
0
 

Accepted Solution

by:
plushey earned 400 total points
ID: 8128250
Isn't sprintf a little OTT?

If I were to use sprintf then why not sprintf(cpath, "%s/", currpath);
however I much prefer
strcpy(cpath, currpath);
strcat(cpath, "/");
Why? because I don't like using sledgehammers to crack nuts :-)

Also if this function is recursive then you really don't want to be using malloc etc... without a call to free before returning.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8128323
plushey,
yes u r true ..actually  we(me and ex)  were racing to post the answer fast , so never checked for shortcuts.. :),
good for me that exceter didnt get this , that cud have saved seconds to him.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8129020
>> good for me that exceter didnt get this , that cud have saved seconds to him.

:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question