1

I am working my way through the K&R C programming language and am currently on Chapter 5 (Pointers). I'm having an issue with Exercise 5-3:

"Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies string t to the end of s."

I wrote the code below:

#include<stdio.h>

void pointer_strcat(char *stringOne, char *stringTwo);

main(){

char *initialString = "Flash. ";
char *middleString = "Welcome.";  
char *addedString  = "Thunder.";

pointer_strcat(initialString, addedString);
printf("%s \n", initialString);

} 

void pointer_strcat(char *stringOne, char *stringTwo){

while(*stringOne++ != '\0'){
}
*stringOne--;
while( (*stringOne = *stringTwo) != '\0'){
    stringOne++;
    stringTwo++;
}
}

I tried to follow the examples in the chapter closely but whenever I run this code, there is a freeze and I get a popup that says the .exe stopped working before returning to the command prompt. I messed around with the code every which way and found that if I change the declarations of string pointers to arrays (*initialString becomes initialString[]), everything works exactly how I want it to. I am particularly vexed by this because on page 83, it says:

"As formal parameters in a function definition, char s[]; and char *s; are equivalent;..."

I could really use some advice on why these different declarations have such radically different performance when running the .exe even though both versions compile.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Yusif_Nurizade
  • 133
  • 1
  • 12
  • Properly indenting your code is always a good start. – Jonathon Reinhart Jul 24 '14 at 17:10
  • @dohashi Great find. Spot-on. – Jonathon Reinhart Jul 24 '14 at 17:11
  • I'm sorry but I have a followup question to the response provided and cannot add a comment until I reach 50 rep. The accepted solution is that the pointer declarations don't allocate enough memory and that arrays big enough to contain both strings are required. I believe the solution goes array1[20], array2[20]. Why does my declaration of no size arrayOne[] work? Is there a default that is large enough? – Yusif_Nurizade Jul 24 '14 at 21:44
  • @Yusif_Nurizade If you mean something like this `char arrayOne[] = "hello world";` that would be because you are allowed to declare an array with no size if you initialize it since the compiler will determine the space needed by the number of initializers provided. – Shafik Yaghmour Jul 25 '14 at 01:37
  • @ShafikYaghmour thank you for the response. How is the compiler's determination of the space needed different between char arrayOne[] = "hello world"; and char *arrayOne = "hello world";? The part that has me stuck is that both give the same number of initializers yet one works and the other does not. – Yusif_Nurizade Jul 25 '14 at 15:01
  • @Yusif_Nurizade as I explain [here](http://stackoverflow.com/a/24874210/1708801) the declaration `arrayOne = "hello world";` converts a string literal to a pointer to a string literal which is not modifiable. The second case you have an array and the string literal is copied to it. – Shafik Yaghmour Jul 25 '14 at 15:11
  • @ShafikYaghmour, I guess it's one of those things I will just have to accept. I get that one is modifiable and the other not by their definitions but I keep getting thrown by the fact that the book says the declarations are the same. Thanks for trying. – Yusif_Nurizade Jul 25 '14 at 17:24

0 Answers0