C - Strings in Structures returning whole array -


i'm trying store characters read file array of characters, ends storing every succeeding character well. code, created structure , function initialize array.

from "structures.h"

typedef struct (     int size;     char *elem;     } cvector; 

from "utilities.c"

cvector make_cvector (int size)     {   cvector temp;         temp.size = size;         temp.elem = calloc(size, sizeof(char));         return temp;     } 

then i'm trying read text file says: "a b c"

in body, stated: (where nptr pointer opening file)

cvector nodeid; nodeid = make_cvector(3); for(i=0;i<3;i++){         fscanf(nptr,"%s", &nodeid.elem[i]);         printf("%s ",&nodeid.elem[i]); } 

this results in " b c " right after loop, typed loop:

for(i=0;i<3;i++)         printf("%s ", &nodeid.elem[i]); 

resulting "abc bc c" when in fact want "a" "b" , "c" stored separately. there's wrong initialization or pointers, i've been trying read online no avail. error be? thank you!

you printing string when want printing single character. replace

printf("%s ",&nodeid.elem[i]); 

by

printf("%c ",nodeid.elem[i]); 

and should work.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -