Re: Struggling with libraries
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Apr 2006 20:26:48 +0100
Tony Burrows wrote:
On Mon, 17 Apr 2006 18:46:44 +0200, Nils O. Selåsdal wrote:
Tony Burrows wrote:I've called it from a simple little program:I'm just learning C as another language, and I'm trying to build someIt's illegal to return pointers to auto objects.
utilities into a library. I have this (crude I know) function:
void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1]; strncpy(temp, source, place);
temp[place]='\0';
strcat(temp, insrt);
strcat(temp, source+place);
strcpy(source, temp);
}
In a simple file, it all works, but in a static library, when I call it I
get a segnmentation fault. The villain is the final strcpy.
I tried returning temp instead and that doesn't work either, I don't get
quite the string that exists in the function (and the compiler warns about
returning a local variable).
I know there's a solution, I just have no idea what it is. Any help wouldWIthout seeing how it is beeing called, hard to tell.
be great.
Be very very sure your 'source' has enough room for
'insrt'.
#include <stdio.h>
#include "insert.h"
int main(void){
char *str1 = "You are a nutcase!";
char *str2 = "big ";
puts(str1);
puts(str2);
insert(str2, str1, 10); return 0;
}
insert.h is
#include <string.h>
void insert(char* insrt, char* source, int place);
void insertn(char* insrt, char* source, int place, int size);
Compiled with gcc -o testInsert testInsert.c insert.c
If I put print statements in, then the segmentation fault happens in the
insert function when strcpy(source, temp) is executed.
If I created a string on the heap with (I think) malloc and returned a
pointer to that, would that deal with the problem? If so, is it the right
way? The C programming books don't seem to touch on this.
I'm sure they do. The comp.lang.c FAQ certainly does with a much simpler example here http://c-faq.com/strangeprob/strlitnomod.html which tells you that string literals are not modifiable.
In addition, even if string literals were modifiable, you are trying to make the string longer, so this would not work either:
#include <stdio.h>
#include "insert.h"
int main(void){
char str1[] = "You are a nutcase!";
char *str2 = "big ";
puts(str1);
puts(str2);
insert(str2, str1, 10);
return 0;
}
Since it would go off the end of str1.
If I was writing a function like your insert I would probably make is malloc enough space for a new string and return the resultant string in that rather than modifying one of the input strings.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.
- Follow-Ups:
- Re: Struggling with libraries
- From: Tony Burrows
- Re: Struggling with libraries
- References:
- Struggling with libraries
- From: Tony Burrows
- Re: Struggling with libraries
- From: "Nils O. Selåsdal"
- Re: Struggling with libraries
- From: Tony Burrows
- Struggling with libraries
- Prev by Date: Re: Struggling with libraries
- Next by Date: Re: Struggling with libraries
- Previous by thread: Re: Struggling with libraries
- Next by thread: Re: Struggling with libraries
- Index(es):
Relevant Pages
|
Loading