S
svata
Hello there,
after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
char *p_item_name;
printf("Enter the description: ");
/* we want no more chracters than size!!! */
if (fgets(p_item_name_1, size, stdin) != NULL){
/* if the input contains a new line */
if (( p_item_name = strchr(p_item_name_1, '\n')) != NULL ){
*p_item_name = '\0'; /* get rid of new line, pointer p_item_name
points to it*/
}
else {
while(getchar() != '\n'){
;
}
}
strcpy(p_buf, p_item_name_1); /* and now copy it to the buffer
provided by caller */
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}
in the main might be something like this:
char *p_new_buf;
char *buf;
int size = 10;
/* and here we can malloc buffer */
p_buf = malloc(sizeof(char) * size);
svata
after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
char *p_item_name;
printf("Enter the description: ");
/* we want no more chracters than size!!! */
if (fgets(p_item_name_1, size, stdin) != NULL){
/* if the input contains a new line */
if (( p_item_name = strchr(p_item_name_1, '\n')) != NULL ){
*p_item_name = '\0'; /* get rid of new line, pointer p_item_name
points to it*/
}
else {
while(getchar() != '\n'){
;
}
}
strcpy(p_buf, p_item_name_1); /* and now copy it to the buffer
provided by caller */
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}
in the main might be something like this:
char *p_new_buf;
char *buf;
int size = 10;
/* and here we can malloc buffer */
p_buf = malloc(sizeof(char) * size);
svata