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);
free(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]);
    lower_bound_of_big_boy_counter++;

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

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

Dani AI

Generated

The warning comes from this call: you are passing &temp to a function that expects char *. temp is an array (char temp[80]). In C, when you pass an array to a function it automatically decays to char *. Taking its address produces a different type, char (*)[80], hence the incompatible pointer warning. Call it with temp, not &temp, and do not forget the semicolon.

You also append elements 1, 2, and 3 even though only 0 and 1 are initialized. That is undefined behavior and will bite quickly. Either initialize those elements or pass how many tokens you actually have, and bound the concatenation so temp cannot overflow. A small, safer helper looks like this:

void cat(char *dst, size_t dstsz, char *tokens[], size_t n)
{
    for (size_t i = 0; i < n && tokens[i]; ++i) {
        size_t room = dstsz - 1 - strlen(dst);
        if (room == 0) break;
        strncat(dst, tokens[i], room);
    }
}

Example usage after copying the first token into temp:

/* append tokens[1] and tokens[2] if both are set */
cat(temp, sizeof temp, &strings_line_tokens[1], 2);

A few cleanup tips, building on ’s points: if you allocate strings for strings_line_tokens[i], remember to free them before main returns. strdup is handy for allocation-plus-copy in one step. And because str1 and str2 are fixed arrays, swapping their contents (as you do) is the right approach; just ensure the destination buffers are large enough and consider a fixed-size stack buffer for swap2 to avoid repeated malloc/free.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.