0
#include <stdio.h> #include <string.h> void mergeStr(char *a, char *b, char *c); int main() { char a[80],b[80]; char c[80]; printf("Enter the first string: \n"); gets(a); printf("Enter the second string: \n"); gets(b); mergeStr(a,b,c); printf("mergeStr(): "); puts(c); return 0; } void mergeStr(char *a, char *b, char *c) { int size; int i ; int j=0 ; // again, forgot to initialize j char ch; char temp[80] ; /* Merge string a and string b together, then sort them alphabetically */ c = strcat(a,b) ; size = strlen(c) ; for (ch = 'A' ; ch <= 'z' ; ch++ ) { // iterates from A-Z and a-z for (i=0 ; i<size ; i++) { // which for loop comes first is important, in this case since we want duplicates we should allow i to iterate everything for every case of ch if (c[i] == ch){ temp[j] = c[i] ; j++ ; } } } for (i=0 ; i<size ; i++) { c[i] = temp[i] ; // assign sorted string back to c c[size] = '\0' ; } // puts(c) <- when puts() is run here, desired output is given } 

In this program, the function takes char a , concatenates it with char b, which is assigned to c.

char c is then sorted and printed out in the main function by puts(c).

For example,

Enter the first string: afkm Enter the second string: bbbggg abbbfgggkm mergeStr(): 

This is the output i get when puts(c) is run from within void mergeStr() function.

However, puts(c) from int main() does not print anything.

1
  • Don't use gets(), it's a deprecated old dangerous function. Commented Mar 16, 2018 at 13:15

1 Answer 1

4

This:

/* Merge string a and string b together, then sort them alphabetically */ c = strcat(a,b) ; 

doesn't do what you seem to expect, it doesn't write into the caller's (i.e. main()'s) buffer at all. It overwrites the value of c (the function argument) with the return value of strcat(), which will be the destination, i.e. a.

You need to read up on how C strings work, and how the memory holding them is handled.

That particular call could be replaced by:

sprintf(c, "%s%s", a, b); 

but this is dangerous and risks overwriting buffers since no size information is passed into the function (so it can't use snprintf()).

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

8 Comments

Does this mean that c has become a local variable after strcat()?
This is a little confusing though, why would strcat() make c a local variable? In other words, say i created another temp char[80] and did temp = strcat(a,b) and then later assigned it into c, my code would work? Sorry if my explanation is unclear.
@JoeyNgo c is the name of the third argument to the function, and inside the function that name refers to the value of the third argument. Nothing happens with the variable referred to as c in another scope, i.e. in main(), and of course strcat() itself is not doing anything like that. For your final question: you cannot assign to arrays, so that would never work no.
hmm, from what i understood, since the parameter is *char c, wouldn't changes made in this void function change in main as well? Since it is a pointer and I am changing the same address location where c was created (in main)
@JoeyNgo *c = something (or some other dereference such as sprintf) would indeed change what the buffer contains. But c = something re-points the local pointer c.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.