I'm having trouble getting the parameters correct in my cat function. I would think since I'm changing temp that I would need to pass the address since I want to be able to see that in main. I think I'm getting screwed up by the decaying.

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

/* Swaps strings by swapping data*/
void swap2(char *str1, char *str2)
char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char));
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);

void cat(char *temp, char *strings_line_tokens[])
    strcat (temp,strings_line_tokens[1]);
    strcat (temp,strings_line_tokens[2]);
    strcat (temp,strings_line_tokens[3]);

int main()
    char str1[10] = "geeks";
    char str2[10] = "forgeeks";
    char temp[80] = {0};
    char *strings_line_tokens[503] = {0};
    int lower_bound_of_big_boy_counter = 0;

    strings_line_tokens[0] = malloc(strlen("string")+1);
    strcpy(strings_line_tokens[0], "string");

    strings_line_tokens[1] = malloc(strlen("string1")+1);
    strcpy(strings_line_tokens[1], "string1");

    strcpy (temp,strings_line_tokens[0]);

    cat(&temp, strings_line_tokens)
    printf("temp is %s", temp);

    swap2(str1, str2);
    printf("str1 is %s, str2 is %s", str1, str2);
    return 0;

Can someone fix my title? I don't see an edit option.

warning: passing argument 1 of 'cat' from incompatible pointer type [enabled by default]

Can't fix the title, but there are a number of issues in your code, and I expect you could find them after a nice drink and good night's sleep... :-) Anyway look at your array indices in cat(). Also, instead of malloc'ing everything and then copying the data, try that nice function strdup(). It will take care of the allocation and copying for you. Also, in your declaration of strings_line_tokens[], you are only setting one element, and you only populate the 0th and 1st elements with real strings in main, yet in cat() you are calling strcpy on elements 1, 2, and 3. It should be 0 and 1 as 2 and 3 will either be null pointers, or just bogus data, depending upon your compiler. In any case, this is where things go belly up, or as we say in the business, you let the smoke out!

So, apply these suggestions to your code, and repost after you have fixed it. I'll be happy to look again then.

This article has been dead for over six months. Start a new discussion instead.