0

I am trying to return a string from my sCopy() function, so I can print it in the main() function of my code. Please help.

const char *sCopy(char buffer[256], int i); int main() { int i; int x; char buffer[256]; char newBuffer;//[256]; printf("Please enter a number: "); fgets(buffer, 256, stdin); i = atoi(buffer); printf("The value you entered is %d. Its double is %d.\n", i, i*2); newBuffer = sCopy(buffer, i); printf(newBuffer); return 0; } const char *sCopy(char buffer[256], int i){ char nBuffer[256]; char *t; int x; for(x = 0; x < i; x++){ strcat(t, buffer); } //t = nBuffer; return t; } 
4
  • sCopy return pointer to char and newBuffer is just a char. Commented Mar 15, 2013 at 3:19
  • 2
    whats the point to use for loop when u dont use index "x" at all? Commented Mar 15, 2013 at 3:21
  • I using gcc, declarations are not allowed in the for loop, not unless you compile with flags. Commented Mar 15, 2013 at 3:33
  • Use t = strndup(buffer,i); Commented Mar 15, 2013 at 7:42

5 Answers 5

1

My analysis below. From what I see here you should brush up on the difference between a pointer to char (char*) and a char array. I really appreciate though that you've tried to solve it yourself before asking.

const char *sCopy(char buffer[256], int i); /* let's start from here, what i represents? Keep in mind that the most of the */ /* time an external developer will see just your declaration of a method, always */ /* try to give significant names to variables. */ int main() { int i = 0; /* always initialize variables to default values, especially if they are */ /* going to be indexes in a buffer. */ int x = 0; char buffer[256] = ""; /* you can even initialize this to "" in order to mimic an empty string, */ /* that is a char array cointining just \0 (string null-terminator). */ char newBuffer[256] = ""; /* same here, you always need to declare the size of a char array unless */ /* you initialize it like this -char newBuffer[] = "hello"-, in which case */ /* the size will automatically be 6 (I'll let you discover/understand */ /* why 6 and not 5). */ printf("Please enter a number: "); fgets(buffer, 256, stdin); // nice link at the bottom on input reading i = atoi(buffer); printf("The value you entered is %d. Its double is %d.\n", i, i*2); newBuffer = sCopy(buffer, i); printf(newBuffer); return 0; } /* I am not going to debate the idea of returning a char pointer here :) */ /* Remember that in this case you are returning a pointer to some memory that has */ /* been allocated somewhere inside your function and needs to be released (freed) */ /* by someone outside your control. Are they going to remember it? Are they */ /* going to do it? In this case "they" is "you" of course. */ /* I'll let you explore alternative approaches. */ const char *sCopy(char buffer[256], int i){ char nBuffer[256] = ""; // see above char *t = NULL; /* you always init a pointer to NULL. As you can see, initializing here will */ /* make you think that there might be problem with the strcat below. */ int x; // ok here you can omit the init, but be aware of it. for(x = 0; x < i; x++){ strcat(t, buffer); /* what are you missing here? this piece of code is supposed to concatenate the */ /* input buffer to a brand new buffer, pointed by your variable t. In your implementation */ /* t is just a pointer, which is nothing more than a number to a memory location */ /* With the initialization, the memory location you are pointing to is NULL, which */ /* if de-referenced, will cause massive problems. */ /* What you are missing is the blank slate where to write your data, to which your */ /* pointer will read from. */ } //t = nBuffer; return t; } 

I really hope that this would help you. I am sorry but I can't write the solution just because I think it's better if you learn it the hard way. You can find plenty of tutorials of pointers to char and I am sure you will solve the problem.

(input reading) C scanf() and fgets() problem

Sign up to request clarification or add additional context in comments.

Comments

0

Memory for char *t is not allocated. You can implement the string copy directly using strdup.

char *newBuffer = NULL; ... ... newBuffer = strdup(buffer); 

Comments

0

Try

char* newBuffer; // point to a c-string // here the return value of sCopy 

and

static char nBuffer[256]; // make nBuffer survive beyond function call 

and just use n return nBuffer, instead of t.

This is one way you could do it, without allocations. Alternatively allocate memory and return it instead of static char.

Comments

0

Try below:

#include <string.h> #include <stdio.h> #include <stdlib.h> void sCopy(char buffer[256], int i, char newBuffer[], int size) { char *t; for(int x = 0; x < i; x++){ strcat(newBuffer, buffer); } } int _tmain(int argc, _TCHAR* argv[]) { int i; char buffer[256] = {0}; char newBuffer[256] = {0}; printf("Please enter a number: "); fgets(buffer, 256, stdin); i = atoi(buffer); printf("The value you entered is %d. Its double is %d.\n", i, i*2); sCopy(buffer, i, newBuffer, 256); printf("%s", newBuffer); return 0; } 

Note that fgets appends a new line character at the end of the input string, so if you input 6 then the string is 0x36 0x0a where 0x36 is the ASCII code for 6 and 0x0a is the ASCII code for new line character, to remove this new line character, see this link

Comments

0
#include <stdio.h> #include <stdlib.h> #include <string.h> const char *sCopy(char buffer[256], int i); int main() { int i; int x; char buffer[256]; const char* newBuffer;//[256]; printf("Please enter a number: "); fgets(buffer, 256, stdin); i = atoi(buffer); printf("The value you entered is %d. Its double is %d.\n", i, i*2); newBuffer = sCopy(buffer, i); //buffer tail '\n' need cut? printf(newBuffer); free(newBuffer); return 0; } const char *sCopy(char buffer[256], int i){ char *t; int x; t=(char*)malloc(strlen(buffer)*i + 1); *t='\0'; for(x = 0; x < i; x++){ strcat(t, buffer); } //t = nBuffer; return (const char*)t; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.