-1

[Warning] passing argument 1 of 'strcat' makes pointer from integer without a cast THIS ERROR IS COMING WHEN I AM USING STRCAT WHAT SHOULD I DO , below is my code

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

void main() {
    char a[20], b[256], p = NULL, f;
    int i, j, n, k, c[20], t, x, l;
    printf("enter the no of possible characters");
    scanf("%d", &k);
    printf("enter the possible characters in the dictionary");
    printf("hi");
    for (i = 0; i < k; i++) {
        c[i] = (i);
        a[i] = getchar();
    }
    for (i = 0; i < k; i++) {
        printf("%d  %s\n ", c[i], a[i]);
    }
    l = k;

    printf("enter the string\n");
    scanf("%s", &b);
    n = strlen(b);
    printf("%d", n);
    for (i = 0; i < n; i++) {
        strcat(p, b[i]);
    }
    getch();
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Rachit
  • 7
  • 1

2 Answers2

3

This is because strcat takes null-terminated strings, not individual characters. You can work around this problem by null-terminating b at position n, and calling strcat once:

b[n] = '\0';
strcat(p, b);

This will append the initial n characters of b to p, all in one go. Of course, given that p does not have any characters in it, this is completely pointless: you might as well use strcpy. Of course, you need to declare p to be a character buffer large enough to hold all of b:

char a[20],b[256],p[256]={0},f;
//                 ^^^^^^^^^
b[n] = '\0';
strcpy(p, b);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Your variable definition is wrong.

  char a[20],b[256],p=NULL,f;

does not define p as a pointer. It is a char variable with is not the expected type for the first argument of strcat().

You need to define p as a pointer to a string which has enough memory to hold the concatenated result.

That said,

 strcat(p,b[i]);

is completely wrong, because b[i] is not a pointer to a string, either.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261