Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what does this mean??

Reply
Thread Tools

what does this mean??

 
 
chutsu@gmail.com
Guest
Posts: n/a
 
      05-27-2007
I got errors:
qu2-1.c:66: warning: format '%s' expects type 'char *', but argument 2
has type 'char (*)[19u]'
qu2-1.c:72: warning: format '%s' expects type 'char *', but argument 2
has type 'char (*)[19u]'

but I don't quite know what it means...
<code/>

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

#define MAXPATIENTS 20

struct details {
int id;
char forename[20];
char initial;
char surname[20];
int day_of_entry;
int max_wait;
};

struct details patient[MAXPATIENTS];
int npatients = 0;

//Functions
int day_now(void);
int read_queue(void);
int list_patient(int index);
int queue_patient(struct details newpatient);
void list_queue();
void write_queue();
int insert_patient(int index, struct details newpatient);
int delete_patient(int index);
int find_patient_id(int id);


//Main starts here
int main (void) {
int Choice;
int id;
int index;
int n;


//Loop that continues the program
while(n < 0){
//To keep program running
//Displaying Menu
printf("\n\n");
printf("_________________________________________\ n");
printf("|-----------NHS Queue Control-----------|\n");
printf("| Create New Patient \t- Press 1\t|\n");
printf("| Delete Patient \t- Press 2\t|\n");
printf("| Find Patient \t\t- Press 3\t|\n");
printf("| List Queue \t\t- Press 4\t|\n");
printf("| Quit \t\t\t- Press 5\t|\n");
printf("|_______________________________________|\ n");
printf("Choice: ");
scanf("%d",&Choice);
printf("\n");
printf("%d \n",day_now());
//Choice made, carrying out function
switch (Choice){
//New Patient
case 1:
index = read_queue() + 1;
printf("Please enter the following details\n");
printf("Patient ID: ");
scanf("%d", &patient[index].id);
printf("Forename: ");
scanf("%s", &patient[index].forename);
printf("\n");
printf("Middle Initial: ");
scanf("%c", &patient[index].initial);
printf("\n");
printf("Surname: ");
scanf("%s", &patient[index].surname);
printf("\n");
printf("Day of Entry (Since 1/1/1970): ");
scanf("%d", &patient[index].day_of_entry);
printf("Maximum Waiting Time: ");
scanf("%d", &patient[index].max_wait);
insert_patient(index, patient[index]);
break;
........

int insert_patient(int index, struct details newpatient) {
//For testing only.....
printf("%d \n",patient[index].id);
printf("%s \n",patient[index].forename);
printf("%c \n",patient[index].initial);
printf("%s \n",patient[index].surname);
printf("%d \n",patient[index].day_of_entry);
printf("%d \n",patient[index].max_wait);
}

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-27-2007
In article <. com>,
<> wrote:
>I got errors:
>qu2-1.c:66: warning: format '%s' expects type 'char *', but argument 2
>has type 'char (*)[19u]'
>qu2-1.c:72: warning: format '%s' expects type 'char *', but argument 2
>has type 'char (*)[19u]'


Those are warnings, not errors.

>struct details {
> int id;
> char forename[20];
> char initial;
> char surname[20];
> int day_of_entry;
> int max_wait;
>};


Notice that forename and surname are fixed length arrays of up
to 20 characters. A fixed length character array does not necessarily
end in a null byte ('\0'), but you are attempting to print the
names using %s format, which is reserved for character arrays that
*do* end in a null byte. The compiler is warning you that this
might be an incompatability -- if you had a forename or surname
which used all 20 characters of the array, then printing that
forename or surname with %s would "fall off the end" of the character
array with undefined results.

If you are -certain- that forename or surname will end in '\0' then you
can either ignore the warnings, or you can cast the type in the
printf() calls.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-27-2007
(Walter Roberson) writes:
> In article <. com>,
> <> wrote:
>>I got errors:
>>qu2-1.c:66: warning: format '%s' expects type 'char *', but argument 2
>>has type 'char (*)[19u]'
>>qu2-1.c:72: warning: format '%s' expects type 'char *', but argument 2
>>has type 'char (*)[19u]'

>
> Those are warnings, not errors.
>
>>struct details {
>> int id;
>> char forename[20];
>> char initial;
>> char surname[20];
>> int day_of_entry;
>> int max_wait;
>>};

>
> Notice that forename and surname are fixed length arrays of up
> to 20 characters. A fixed length character array does not necessarily
> end in a null byte ('\0'), but you are attempting to print the
> names using %s format, which is reserved for character arrays that
> *do* end in a null byte. The compiler is warning you that this
> might be an incompatability -- if you had a forename or surname
> which used all 20 characters of the array, then printing that
> forename or surname with %s would "fall off the end" of the character
> array with undefined results.
>
> If you are -certain- that forename or surname will end in '\0' then you
> can either ignore the warnings, or you can cast the type in the
> printf() calls.


The warnings were on the scanf() calls, not the printf() calls.
(chutsu: it would have saved time if you had told us that yourself.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-27-2007
wrote:
>
> I got errors:
> qu2-1.c:66: warning: format '%s' expects type 'char *', but argument 2
> has type 'char (*)[19u]'
> qu2-1.c:72: warning: format '%s' expects type 'char *', but argument 2
> has type 'char (*)[19u]'
>
> but I don't quite know what it means...
> <code/>


> struct details {
> int id;
> char forename[20];


> struct details patient[MAXPATIENTS];
> int npatients = 0;


> scanf("%s", &patient[index].forename);


(patient[index].forename) is the name of an array of 20 char.
(array of 20 char) is the type of the expression.
An expression of array type, converts to a pointer to its first element
in all but three cases. So,

scanf("%s", patient[index].forename);

is the right way to write that.

(&patient[index].forename) is the address of an array of 20 char.
(pointer to an array of 20 char) is the type of the expression.
When the name of an array is the operand of the & operator,
is one of the cases where the name of an array doesn't
automatically convert to a pointer.
When it's the operand of sizeof is another case
where the name of an array doesn't
automatically convert to a pointer.
The third case, is when a string literal initializes an array.

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 ASPx Page does not load, but HTM does prabhupr@hotmail.com ASP .Net 1 02-08-2006 12:57 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments