Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

newbie: chars help

ioriiori Member Posts: 59
[code]
typedef struct
{

pid_t processID; //process ID
int operationMode; //open-read-write-close-lock-unlock
int fdOfFile;
char *message; //message to be appended
//char message[50];

}PCB;

PCB newPCB;
char *message;
scanf("%s",&message);
printf("
Message: %s",&newPCB.message);[/code]

can anyone please tell me why do i get only the first 4 characters of the input after doing the printf. i do get the right input if i am directly printing the "message" variable like this :
[code]printf("
Message: %s",&message);[/code]

thanks

Comments

  • MT2002MT2002 Member Posts: 1,444
    : [code]
    : typedef struct
    : {
    :
    : pid_t processID; //process ID
    : int operationMode; //open-read-write-close-lock-unlock
    : int fdOfFile;
    : char *message; //message to be appended
    : //char message[50];
    :
    : }PCB;
    :
    : PCB newPCB;
    : char *message;

    [red]// scanf() skips white space characters!
    // try gets () instead.[/red]
    scanf("%s",&message);
    : printf("
    Message: %s",&newPCB.message);[/code]
    :
    : can anyone please tell me why do i get only the first 4 characters of the input after doing the printf. i do get the right input if i am directly printing the "message" variable like this :
    : [code]printf("
    Message: %s",&message);[/code]
    :
    : thanks
    [blue]scanf () reads input until either an EOL charater
    is reached, or any white space character. So, if this
    is your input:[/blue][code]
    Hello, World[/code][blue]
    scanf () will only read the first 6 bytes ([green]Hello,[/green]).

    You can try using gets () instead. gets () reads input
    until EOL or EOF:[/blue][code]
    gets (message);[/code]
Sign In or Register to comment.