|
From: Richard Heathfield on 2 Jan 2008 10:32 Rui Maciel said: > I want to convert the text contained in a c-string but I'm not getting > much success. I've tried the following function: > > int test(char *input, char *output); This doesn't really tell us much. What storage do you have, what is the content of the input, and what manipulations are you trying to do on it? > I'm able to read the information to the function but it isn't possible to > access the modifications done to output from outside the function. This program demonstrates a function that makes a modification to a string. #include <stdio.h> #include <string.h> int test(const char *input, char *output); int main(void) { const char *in = "hello, world"; char out[32] = "this will be overwritten"; int vowels; vowels = test(in, out); printf("[%s] (%d)\n", out, vowels); return 0; } int test(const char *input, char *output) { int count = 0; /* copy all vowels from input to output */ while(*input != '\0') { if(strchr("aeiouAEIOU", *input) != NULL) { *output++ = *input; ++count; } ++input; } *output = '\0'; return count; } Expected output: [eoo] (3) Actual output: [eoo] (3) > So, > what do I need to do to be able to tweak output as much as I would like, As long as you own the storage, you can do what you like with it! -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: Jim Langston on 2 Jan 2008 10:40 Rui Maciel wrote: > I want to convert the text contained in a c-string but I'm not > getting much success. I've tried the following function: > > int test(char *input, char *output); > > I'm able to read the information to the function but it isn't > possible to access the modifications done to output from outside the > function. So, what do I need to do to be able to tweak output as much > as I would like, even calloc'ing a new string, and still be able to > access that info from outside the function? What is it you are attempting? To take the c-style string pointed to input and copy it to output allocating memory for it? char* output is a pointer copied by value. To be able to change where the pointer is pointing to you'll need to either change the parameter to a pointer to a pointer char** output, or prefered in C++ to change it to a reference to a pointer char*& output, then you can change the pointer. I.E. something like int test(char* input, char*& output) { output = new char[strlen(input) + 1]; strcpy( output, input ); return something; } However, why bother to pass output as a pointer and not return it? I see you are retruning an int, but it would make more sense to return the char pointer. char* test(char* input) { char* output = new char[strlen(input)+1]; strcpy( output, input ); return output; } Now, the question becomes, why are you even needing to do this? Since you are passing a char* in the first place, why not just use that char* in the code rather than copying it to a newed c-style string? You shouldn't have to use c-style strings at all. Why are you attempting to do this? -- Jim Langston tazmaster(a)rocketmail.com
From: Richard Heathfield on 2 Jan 2008 10:56 Jim Langston said: <snip> > I.E. something like > > int test(char* input, char*& output) This is illegal in C. (The subject line makes it clear that the question is about C, not C++.) -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: Francis Glassborow on 2 Jan 2008 11:06 Richard Heathfield wrote: > Jim Langston said: > > <snip> > >> I.E. something like >> >> int test(char* input, char*& output) > > This is illegal in C. (The subject line makes it clear that the question is > about C, not C++.) > Yes indeed (too often people forget to identify which language). Now it rather depends what manipulation is being done to the string. As long as the manipulation does not make the string longer I would first copy the original and then pass the copy to the function doing the manipulation. My reason is that I do not like using malloc and siblings in a function that is not also responsible for freeing the memory when it is finished with. So my code would look something like: int main(){ char astring[Somesize]; initarray(astring); char * bstring = malloc(strlen(astring) + 1); strcpy(bstring, astring) manipulate(bstring); // other code free(bstring); return 0; }
From: Ben Bacarisse on 2 Jan 2008 11:34 Rui Maciel <rui.maciel(a)gmail.com> writes: > I want to convert the text contained in a c-string but I'm not getting much > success. I've tried the following function: > > int test(char *input, char *output); > > I'm able to read the information to the function but it isn't possible to > access the modifications done to output from outside the function. So, what > do I need to do to be able to tweak output as much as I would like, even > calloc'ing a new string, and still be able to access that info from outside > the function? If the allocation is happening in 'test', then I can guess what is going wrong. For a C function to allocate storage and return a pointer to via a parameter you need: int test(char *input, char **output) { *output = malloc(... size ...); if (*output) { char *out_str = *output; /* put stuff in the bytes pointed to by out_str ... */ ... } } To call it, you need to pass a pointer to a 'char *': char *result; if (test("abc", &result) != 0) /* use 'result' here */ But, as has been suggested in the C++ reply, it may be better to return the pointer to the new string. Of course, if the allocation is happening outside 'test' and you are not seeing the results though the 'output' pointer, then you need to post more code. -- Ben.
|
Next
|
Last
Pages: 1 2 3 Prev: [C] edit c-string passed as a parameter Next: Optimizing C/C++ Code. |