-3

I am just learning C and got to the point of pointers. I wrote a method which should concate two char pointers and I got a strange behavior I just cant explain to myself nor did I find an answer on it, so maybe you can help!

So I don't understand why stringcat and stringptr point to different addresses, namely ones that are 8 Byte appart. Secondly, I dont understand why I cannot use stringcat in the two while-loops on the bottom. I just dont get any output. Also if I use stringptr[0] in the last printf, there won't be any output.

Thank you for your help in advance!

Edit: This code works, I just don't understand why I cant use stringcat on bottom.

Edit2: Noticed some * got missing while copy pasting, so I added them!

char stringcat(const char str1, const char* str2){
  char *ptr1 = str1;
  char *ptr2 = str2;
  int count = 0;

  while(*ptr1 != '\0'){
    count++;
    ptr1++;
  }

  while(*ptr2 != '\0'){
    count++;
    ptr2++;
  }

  char *stringcat = malloc((count+1)*sizeof(char));
  char *stringptr = stringcat;

  printf("%d %d", &stringcat, &stringptr);

  ptr1 = str1;
  ptr2 = str2;

  while(*ptr1 != '\0' && (stringptr++ = ptr1++));
  printf("%c", stringcat[0]);
  while(*stringptr++ = *ptr2++);

  return stringcat;
}
F. Zi
  • 41
  • 8

4 Answers4

0

You are making a number of errors. Some will be reported by the compiler but you have to turn warnings on.

How will stringcat be called and what is it to return? This is the essential (first) question in developing a function.One method is that it gets two strings to concatenate and returns a new string, with the second concatenated to the first. Another method is that it returns the first string with the second concatenated to it.

For the first method, it must allocate new storage that can hold both strings, so that would be newstring= malloc(strlen(str1)+strlen(str2)+1); Don't forget to add 1 for the terminating null of strings.

For the second method, it is essentially the same, except that str1 must itself have been allocated with malloc because your function will call free or realloc to re-allocate the storage needed for the (longer) string.

In both cases, the function will look like char *stringcat(char *str1, char *str2). I think with this input you can write the remainder of the function yourself.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • It actually should have been *stringcat(char *str1, char *str2), some of the * just got lost while copy-pasting the code, sorry for that. As for the malloc part, shouldnt be my implementation reserve the same amount of memory or did I got something very wrong? Thank you for answer! :) I still dont understand why the two pointers point to different addresses in memory, you have a clue? – F. Zi Jul 22 '17 at 14:51
  • How did the `const` get included when doing the cut'n'paste? – Ed Heal Jul 22 '17 at 14:54
0

You are creating a char pointer point to a char. The problem is, you don't assign the char variable to pointer, but the address of the char.

So, you should change your:

char *ptr1 = str1

To:

char *ptr1 =&str1

This is because the &var gets the address of the variable.

Then, you were also using *stringptr++, which increments the value of the char it's pointing to, instead of the address space.

I would highly recommend you to read the K&R book. It's an excellent book for both newbies, and people who already know C. And indeed, one of the things addressed there is exactly this.

Nothing Nothing
  • 126
  • 1
  • 8
0

1)

printf("%d %d", &stringcat, &stringptr);

Print the address of the variable stringcat and stringptr which are different, they are allocated into the stack so, presumibly, you are compiling an x64 bit program, so they are 8 bytes apart.

this

printf("%d %d", stringcat, stringptr);

is what you want.

I just made the minimum amount of change to your code to make it works:

char* stringcat(const char* str1, const char* str2) {
    const char *ptr1 = str1;
    const char *ptr2 = str2;
    int count = 0;

    while (*ptr1 != '\0') {
        count++;
        ptr1++;
    }

    while (*ptr2 != '\0') {
        count++;
        ptr2++;
    }

    char *stringcat = (char *)malloc((count + 1) * sizeof(char));
    char *stringptr = stringcat;

    printf("%d %d", stringcat, stringptr);

    ptr1 = str1;
    ptr2 = str2;

    while (*ptr1 != '\0' && (*stringptr++ = *ptr1++));
    printf("%c", stringcat[0]);
    while (*ptr2 != '\0' && (*stringptr++ = *ptr2++));

    *stringptr++ = '\0';

    return stringcat;
}
Rossi88
  • 99
  • 4
  • This works great thanks! But I still can't change stringptr to stringcat in the two bottom while loops, can you explain to me why it doesnt work? – F. Zi Jul 22 '17 at 15:07
  • An example can explain why: Assume that stringcat value is 0x100 after malloc, if you use it in the while loop stringcat value is incremented (you use ++ operator inside the loop) so you cannot return the stringcat anymore, you must return the original address 0x100 – Rossi88 Jul 22 '17 at 15:13
  • Thank you, that was the explanation I was looking for! – F. Zi Jul 22 '17 at 15:41
0

Pertaining to question 1, stringcat and stringptr do point to the address. It is &stringcat and &stringptr that are different.
By declaring char *var = malloc(...), you are reserving a memory space in the stack, let's say address 30, for the pointer variable, var, and then let var contain the address of whatever was malloc-ed, let's say address 200 (which resides in the heap memory).
When you proceed with char *var2 = var, you reserve another lot in the stack to store the new pointer variable var2, most likely 31. you then assign var2 to hold the address that was contained in var, which was 200.
Therefore, the variable in address 30 and 31 would both contain address 200. By using &var what you are getting is actually 30 instead of the 200 you were expecting. Naturally, &var2 would be different with a value of 31.

For the second question, it is hard to understand what you were looking for as there were errors in the code as reflected by the other answers.

lhhong
  • 116
  • 1
  • 5