0

I get the concatenation of both the strings when 'c' is printed inside the function.But when the address of the pointer is returned and "i" is printed inside main, the output is not proper.

#include <stdio.h>

char* comp(char* s,char* t)
{
int count=0;
char *c;
c=s;
while(*s!='\0')
    s++;
for(;*t!='\0';t++,s++)
     *s=*t;
return c;
}

int main(){
char* i;
char c[]= "hello there";
char f[]="world";
i=comp(&c,&f);
printf("%s",i);
return 0;
}
  • 2
    Possible duplicate of [How to printf a memory address in C](http://stackoverflow.com/questions/30354097/how-to-printf-a-memory-address-in-c) – rocambille Sep 25 '16 at 06:00
  • 1
    To be clear, you are *not* "returning the address of a pointer variable". If you did that you would do `return &c` which you (fortunately) don't do. You are simply returning a pointer. Also, you are calling the function with the wrong arguments. Don't use the address-of operator in the call, just pass the arrays as is and the compiler will decay them to pointers to their first elements. For e.g. the array `f` the expression `f` and `&f[0]` are the same. The expression `&f` is actually wrong here. – Some programmer dude Sep 25 '16 at 06:01
  • You're trying to append data into a most likely read only memory location. And even if it wasn't read only you're still going past the allocated memory, which is undefined behaviour. You don't get to ask "why" when there's undefined behaviour. – Sami Kuhmonen Sep 25 '16 at 06:01
  • As for your problem, please show us the *actual* output, and the *expected* output. And learn how to use a debugger to step through the code line by line while monitoring variables. – Some programmer dude Sep 25 '16 at 06:02
  • Not to mention you're giving a char*[] to a function that expects a char* and... Please set your compiler to show proper warnings and errors. – Sami Kuhmonen Sep 25 '16 at 06:04

1 Answers1

3

Problems I see:

Problem 1

You are not null terminating the concatenated string in comp.

char* comp(char* s,char* t)
{
   int count=0;
   char *c;
   c=s;
   while(*s!='\0')
      s++;
   for(;*t!='\0';t++,s++)
      *s=*t;

   // Need this
   *s = '\0';
   return c;
}

Problem 2

You are calling the function incorrectly. You need to use:

i=comp(c, f); // Not comp(&c, &f)

Problem 3

The most serious problem is that you are writing over memory that you are not supposed to:

When you use:

char c[]= "hello there";
char f[]="world";

c has enough memory to hold the string "hello there" and f has enough memory to hold the string "world". Trying to write beyond those limits is cause for undefined behavior. You could use:

char c[100]= "hello there";
char f[]="world";
i = comp(c, f);

That will be OK since c has enough space for to hold the concatenated string.

Update, in response to OP's comment

char c[]= "hello there";

is equivalent to:

char c[12] = {'h', 'e', 'l', 'l', 'o', ' ', 't', 'h', 'e', 'r', 'e', '\0'};

Appending "world" to that is equivalent to doing the following:

c[11] = 'w';
c[12] = 'o';
c[13] = 'r';
c[14] = 'l';
c[15] = 'd';
c[16] = '\0';

That is cause for undefined behavior since you are writing over elements of the array using out of bounds indices. Even accessing elements of the array using out of bounds indices is cause for defined behavior.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks a ton! But can you explain the problem 3 little elaborately. I dont have much idea about the memory management in c, but i will really like to learn. – Shrikant Mahapatra Sep 25 '16 at 19:37