1

I'm trying to write a function in which two words are appended into a third string, and this function must use malloc(). I'm first writing it in the main before I put it in a function. I have:

int main(void){

    char *word = "Johnny";
    char *word2 = " Boy";
    char *buff = malloc(100 * sizeof(char));
    printf("%d\n", sizeof(buff));
    int ct;
    for(ct = 0; ct < strlen(word); ct++){
        buff = word;
    }
    printf("%s\n", buff);
    printf("the counter is now %d\n", ct);

    buff += ct;
    for(ct; ct < 13; ct++){
        buff = word2;
    }

   printf("%s\n", buff);    
}

I want buff to say "Johnny Boy" but at the end of it, "Johnny" is overwritten, and it just says " Boy"

Derry
  • 371
  • 1
  • 13
  • 1
    Think about what you are doing to `buff` and how words are being copied. If you still don't know, try to read the first section of the book *"K&R C"* or another of the many good introductions to C programming for beginning programmers, which will explain why this is happening for you. – zetavolt Jul 17 '17 at 02:09
  • I've already been reading a book (Sams Hour A Day Series). For this particular question it doesn't give an answer. I wouldn't have come here had I been able to figure it out. – Derry Jul 17 '17 at 02:21
  • I've tried : *buff += ct; to move the pointer ahead, nope – Derry Jul 17 '17 at 02:30
  • There are too many problems in the posted code to address in a comment, and they show lack of understanding of C fundamentals. Suggest to find a [better book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn from. That said, consider that `for (q = 0; q < strlen(word); q++){ ex2 = word; }` never does anything with `q`, and simply assigns the value of `word` to `ex2` repeatedly, without changing anything. – ad absurdum Jul 17 '17 at 02:38
  • Also, note that `*buff += ct;` will not "move the pointer ahead", but increments the value stored at the address held by `buff`; `buff += ct;` will move the pointer ahead. – ad absurdum Jul 17 '17 at 02:39
  • is there anyway at all to finish this off given the code that's already there that I've spent hours writing. Even with buff += ct; for(ct; ct < 13; ct++){ buff = word2; } I'm still coming out with the wrong string – Derry Jul 17 '17 at 02:40
  • I've now noticed that at the end of the loop, if I put: buff -= 7; I do get "Johnny" but I want "Johnny Boy" as the end result – Derry Jul 17 '17 at 02:57

2 Answers2

1

Listen, while we want to help, SO is not really meant to be a classroom environment. Plainly, you're struggling with a fundamental lack of understanding about pointers / string manipulation in C, which is very basic material. Get a BETTER book on C and compare this code to your work, and study on it until you understand the differences and exactly what's being done at each step.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
  char word[] = "Johnny";
  char word2[] = " Boy";
  char *temp;
  char *buff = malloc(32 * sizeof(char));
  if (buff == NULL) {
    puts("allocation error");
    return 1;
  }
  for (int ct = 0; ct <= strlen(word); ct++) {
    buff[ct] = word[ct];
  }
  printf("%s\n", buff);
  temp = buff + strlen(word);
  for (int ct = 0; ct <= strlen(word2); ct++) {
    temp[ct] = word2[ct];
  }
  printf("%s\n", buff);
  free(buff);
  return 0;
}
Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • Search for _dereferencing a pointer in c_ on google. That will help understand the difference between `buff = word` and `*buff = *word` – madD7 Jul 17 '17 at 05:37
1

Okay. The first problem here is you must understand that strings are arrays. You cannot in c say that an array is another array. To be honest there are a lot of problems with this code. The guy above me's code is probably gonna be pretty complicated for you to understand so I will try to give you some more understandable code. One more thing, I won't be using pointers because I haven't mastered them yet.

#define BUF 255

int main( void)
{
    char word1[BUF], word2[BUF], word_full[BUF];
    int ct, length;


    printf( "Input your first word\n" );
    scanf( " %s", &word1);

    printf( "Input your second word." );
    scanf( " %s", &word2);

    length = strlen( word1 );


    for ( ct = 0; ct < length; ct++ )
    {
        word_full[ct] = word1[ct];
        word_full[ ct + length ] = word2[ct];
    }

    word_full[BUF] = 0;

    printf( word_full );

    return 0;
}







    return 0;
}