0

This is an unfinished code for converting alphanumeric characters into Morse code. So far only the character "A" is in set. I can't seem to copy the Morse code string of "a" into the variable "c". The compiler tells me that passing argument 1 of strcpy makes pointer from integer without a cast.

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

int main(){
    char c; /* variable to hold character input by user */
    char sentence[ 80 ]; /* create char array */
    int i = 0; /* initialize counter i */
    const char *a = ".- ";

    /* prompt user to enter line of text */
    puts( "Enter a line of text:" );

    /* use getchar to read each character */
    while ( ( c = getchar() ) != '\n') {
        c = toupper(c);
        switch (c){
            case 'A':
                strcpy(c, a);
                break
            }
        sentence[ i++ ] = c;
    } /* end while */

    sentence[ i ] = '\0'; /* terminate string */

    /* use puts to display sentence */
    puts( "\nThe line entered was:" );
    puts( sentence );
    return 0;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
FrancisDoy
  • 81
  • 1
  • 6
  • 1
    `getchar` returns an `int` (not a `char`), change to `int c;`, and `strcpy` wants a `char *` (not a `char`) as first argument – David Ranieri Jan 21 '15 at 20:06
  • 2
    The target of your `strcpy` as-written is a `char`. It needs to be a `char *` That would be wrong from the get-go. – WhozCraig Jan 21 '15 at 20:06
  • `strcpy` (not strcopy) copies a string to a string, not a string to a char. – bolov Jan 21 '15 at 20:07
  • C is declared a `char`, but then you assign it from `getchar()` which returns an int, and then you pass it to `strcpy()` which expects a char*. Make up your mind--a variable can only have one type. – Lee Daniel Crocker Jan 21 '15 at 20:33

2 Answers2

0

c is a single character, while a is a string (which explains both why c can only hold a single character, and why the compiler is complaining). If you want c to hold a whole string, declare it as such (like you did for sentence).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

You've declared the variable c to have type char:

char c;

Then you're trying to use strcpy(c,a) -- but what type does strcpy expect for its first argument? Here's the signature from the manpage:

char *strcpy(char *dest, const char *src);
Squirrel
  • 2,262
  • 2
  • 18
  • 29