views:

119

answers:

5

Hi, could someone take a quick look at this C code and see why I get the compiler error? It is a function for entering details into a calendar structure, and should create one node, i.e. one 'event' on the calendar.

struct event enter_key(void)
{
        int day,month,year,starttime,endtime,length;
        char* descp;
    struct event* n;

        printf("Enter Day:\n");
        scanf("%d", &day);
        printf("Enter Month:\n");
        scanf("%d", &month);
        printf("Enter Year: \n");
        scanf("%d", &year);
        printf("Enter starttime:\n"); scanf("%d", &starttime);
        printf("Enter endtime:\n");
        scanf("%d", &endtime);
        printf("Enter Description: \n");
        scanf("%s", &descp);


        n=mkevent(day, month, year, starttime, endtime, &descp);

When trying to compile I received this message:

newpro.c:115: warning: passing argument 6 of 'mkevent' from incompatible pointer type

Could anyone tell me if I am declaring the pointer wrongly, or if I should allocate space for the 'descp' pointer, or have I tried to create a node in the structure in the wrong manner?

Thanks for reading, C newbie.

+2  A: 

The pointer should point to a buffer large enough for the value the user enters in the scanf call.

  %s      Matches  a  sequence of non-white-space characters;
          the next pointer must be a pointer to char, and the
          array  must  be  large  enough  to  accept  all the
          sequence and the terminating  NUL  character.   The
          input string stops at white space or at the maximum
          field width, whichever occurs first.

So it could be something like

char descp[ MAX_DESCRIPT ];

scanf("%s", descp);

Though in real-world code you should use one of the techniques for reading strings with cannot result in a buffer overflow ( as well as the possible user requirement to enter descriptions which contain spaces ).

have I tried to create a node in the structure in the wrong manner?

I don't know the signature of mkevent, but I'd expect it to take the pointer to a string buffer (const char*) as description, rather than a pointer to a pointer to the string ( pass in descp rather than &descp).

Pete Kirkham
A: 

the message tells you that your mkevent needs a parameter (in the location of &descp) with different type.

you should check again what does mkevent() needs. it's not char**.

maybe you should change &descp to descp in the line of mkevent - just guess.

Francis
+1  A: 

descp is type 'pointer to char' (char*). In the offending line, you are passing the address of the pointer itself (char**). Remove the '&' in front of the argument.

Oh, and don't use scanf() in production code, especially to read strings. It doesn't do any bounds checking, making buffer overflows likely.

A: 

You don't show the declaration of mkevent, but I'll guess you want to pass desc, not its address. You have:

n=mkevent(day, month, year, starttime, endtime, &descp);

You probably want:

n=mkevent(day, month, year, starttime, endtime, descp);
tpdi
A: 

Giving us the declaration of mkevent() will help, but it's more than likely because you're passing &descp when you should be passing descp.

Ryan Nielson