1

I have a question about putting a * into a concatenated string and having it print out the asterisk. My code takes 2 parameter strings, and needs to return a pointer that is the First string and Second string concatenated BUT I need it to have * inbetween each character. (Ex: String 1: 09876; String 2; 12345 result; 0*9*8*7*6*1*2*3*4*5); My issue isn't with trying to print this result, it's adding the asterisk, especially since C won't let me just declare a * or have a pointer point to *. So I'm a bit stuck. Here's my code

   char* widen_stars(char * str1, char *str2)
{       
    int ls1 = strlen(str1);
    int ls2 = strlen(str2);

    char *str4 = (char *) malloc(strlen(str1) + strlen(str2)+ 29);
    char *p1 = str1;
    char *p2 = str2;
    char *p3 = str4;
    char *asterix;
    *asterix = '*'; //Pointer pointing to *, won't let me though//
        while(*p1 != '\0'){
        *p3 = *p1;
        p1++;
        p3++;
        *p3 = *asterix; // Needs to add an *, after printing the first element.//
        }
        while(*p2 != '\0'){
        *p3 = *p2;
        p2++;
        p3++;
        *p3 = *asterix;
        }
    return str4;
}

I supplied just my function, because this is the only part of my code I get problems. EDIT: The 29 in my Malloc is to keep account of asterisks and the NULL character (The parameter strings have a maximum of 30 characters) I fixed my problem of adding the asterisk thanks to the community, now I get a segmentation fault, this should be fun.

Tcranmer
  • 29
  • 1
  • 10
  • Fyi, `'*'` is the integer literal for the asterisk character. Note the single quotes. – WhozCraig Dec 06 '17 at 23:01
  • `char *asterix; *asterix = *;` has two problems. The pointer is uninitialised, and `*` is not a type of value that can be assigned to where it might point. Perhaps `*asterix = '*';` – Weather Vane Dec 06 '17 at 23:01
  • Ya what I was missing was the single quotes, it fixed my issue (I fixed the initialization problem too, thank you) – Tcranmer Dec 06 '17 at 23:03
  • [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Dec 06 '17 at 23:05
  • 2
    There's no need for `asterisk` to be a pointer. Just `char asterisk = '*';` – Barmar Dec 06 '17 at 23:06
  • Hmmm, when you get an unfortunate segmentation fault XD @Barmar I see, so get rid of (*char), I unfortunately need malloc for my 3rd string (as far as I know) – Tcranmer Dec 06 '17 at 23:09
  • I never said not to use `malloc`, I said not to *cast* the result. `char *str4 = malloc(strlen(str1) + strlen(str2)+ 29);` – Barmar Dec 06 '17 at 23:15
  • When you change `asterisk` to an ordinary variable, you should use `asterisk` instead of `*asterisk` in the rest of the code. – Barmar Dec 06 '17 at 23:16
  • Ya, Im to get rid of (char *) in my malloc because it's repetitive. Using `asterisk` instead of `*asterisk` gives me a warning of comparing without a cast. 29 is the max characters I would need for the asterisks that may be needed, and the NULL character – Tcranmer Dec 06 '17 at 23:17
  • Why are you adding 29 to the sum of the string lengths? – Barmar Dec 06 '17 at 23:18
  • Hint: rewrite using `for()` loops. – wildplasser Dec 06 '17 at 23:23

1 Answers1

1

asterix doesn't need to be a pointer, it's just a single character.

You're not using the correct size when allocating the result string. It needs to have 2 characters for each character in the two original strings -- 1 for each of the characters you're copying, and 1 for the asterisks in between.

You need to add a null terminator to the final result. This can replace the final asterisk, since you're only supposed to have asterisks between the characters, not after each character.

char* widen_stars(char * str1, char *str2)
{       
    int ls1 = strlen(str1);
    int ls2 = strlen(str2);

    char *str4 = (char *) malloc(2 * ls1 + 2 * ls2);
    char *p1 = str1;
    char *p2 = str2;
    char *p3 = str4;
    const char asterix = '*';
    while(*p1 != '\0'){
        *p3++ = *p1++;
        *p3++ = asterix; // Needs to add an *, after printing the first element.//
    }
    while(*p2 != '\0'){
        *p3++ = *p2++;
        *p3++ = asterix;
    }
    // Replace last asterisk with null byte
    if (p3 > str4) {
        p3--;
    }
    *p3 = '\0';
    return str4;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for the answer; with some tweaking it worked (I just had to add a p3++ because it would put the next string character over the asterisk) – Tcranmer Dec 08 '17 at 06:19
  • Yeah, didn't notice the extra increment was missing. I changed it to do the increments along with the assignments. – Barmar Dec 08 '17 at 08:01