1

I'm having a problem with the program below. I'm trying to scan through a string command entered by the user for certain words. My major issue right now is that when I run the following I get a warning saying that "passing arg 2 of `strcat' makes pointer from integer without a cast". My intent is to loop through the first three characters of the string "s", concatenate them onto a string "firstthree", and later check the value of the string "firstthree". Any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>

/* Simple example of using gnu readline to get lines of input from a user.
Needs to be linked with -lreadline -lcurses
add_history tells the readline library to add the line to it's
internal histiry, so that using up-arrow (or ^p) will allows the user
to see/edit previous lines.
*/

int main(int argc, char **argv) {
    char *s;
    while (s=readline("Enter Name: ")) {
            add_history(s); /* adds the line to the readline history buffer */
            printf("Hello %s\n",s);/*output message to the user*/
            char *firstthree;
            int i;
            for(i = 0; i < 3; i++){
                    strcat(firstthree, s[i]);
                    printf("Hello %s\n",firstthree);//checking to see the character added to the end of the string
            }
            printf("Hey %s\n",firstthree);/*prints out the first three characters*/
            free(s); /* clean up! */
            free(firstthree);
    }
    return(0);
}
kame
  • 20,848
  • 33
  • 104
  • 159
Raymond Lemon
  • 63
  • 1
  • 1
  • 6

2 Answers2

6

Your program has a lot of problems; you never initialize firstthree, for example.

The reason you're getting the specific error you're seeing is because of this call:

strcat(firstthree, s[i]);

s is a char *, so s[i] is a char, but strcat expects both parameters to be pointers to null-terminated strings. What it seems you want is something like:

char firstthree[4] = { 0 };
for (int i = 0; i < 3; i++)
{
    firstthree[i] = s[i];
    printf("Hello %s\n", firstthree);
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

You can't use strcat() to do this; it requires two char* s as arguments, not a char* and a char. You could use strncat() if it is available on your platform.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • The signature of `strncat` is `char *strncat(char *dest, const char *src, size_t n);` It also expects two char*s as arguments so it doesn't fix the problem. – markgz Oct 10 '13 at 23:04
  • 1
    Of course it does -- you're kidding, right? He can use it to copy just three characters from s to firstthree , instead of copying them with an explicit loop. – Ernest Friedman-Hill Oct 10 '13 at 23:41
  • 3
    Ok. Considering that this is the first question from a new user, it helps to give a less terse answer :-) – markgz Oct 11 '13 at 00:38