pointer, struct, scanf

This is a discussion on pointer, struct, scanf within the C Programming forums, part of the General Programming Boards category; I have two structs as follow : Code: struct page { //the name of the page can not be more ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    pointer, struct, scanf

    I have two structs as follow :
    Code:
    struct page {
    	//the name of the page can not be more than  9 
    	char name[10];	
    };
    struct edge {
     	struct page *page_start ;
     	struct page *page_end ;
    };
    what I am trying to do is to use scanf to read *(page_start).name from user which it length is max 9 character :
    Code:
    int
    main(void) {
    scanf ("%d",&edges_num);
    edges = (struct edge *) malloc (sizeof(struct edge)*edges_num);
    int i = 0 ;
    for (i = 0 ; i < edges_num ; i ++)
    	{
    	
    		scanf("%9s",edges[i].page_start->name);
    		
      	}
    return 0 ; 
    }
    It seems scanf is not reading user input , can somebody please help ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You probably need to hand it an address in this case...

    Code:
    scanf("%9s",&edges[i].page_start->name);

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Code:
    edges = (struct edge *) malloc (sizeof(struct edge)*edges_num);
    There is absolutely no need to cast the return value of malloc in C, and doing so can hide other issues, such as failing to #include <stdlib.h>

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    I'm surprised it's not segfaulting - you've allocated your memory for the edge structures, but now each of them has a pointer labelled page_start and it's not pointing anywhere - you need to allocate enough memory for that:

    Code:
    for (i = 0 ; i < edges_num ; i ++)
    {
        edges[i].page_start = malloc(sizeof(*(edges[i].page_start)));
        /* check malloc() went okay */
        scanf("%9s", edges[i].page_start->name);
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    by doing this i still get the following compiler error
    <code> warning: format '%9s' expects type 'char *', but argument 2 has type 'char (*)[10]' </code>
    and scanf still do not read the input

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    by doing this
    Doing what? What's the latest code?

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    3
    Gents great tips , this was my very first post and thanks for such a warm welcomeness
    I thought by allocating memory to edges and using a pointer to page in my edge struct there is no need to allocate memory to page pointer in edge struct
    but clearly I was wrong
    by compiling the following code i still get
    Code:
     warning: format '%9s' expects type 'char *', but argument 2 has type 'char (*)[10]'
    latest code is :
    Code:
    for (i = 0 ; i < edges_num ; i ++)
    {
        edges[i].page_start = malloc(sizeof(*(edges[i].page_start)));
        scanf("%9s",&edges[i].page_start->name);
    }
    but it works

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You should generally avoid scanf for reading strings and use fgets instead. Scanf can easily lead to buffer overflows and is terrible at handling white spaces in your input.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 04:38 PM
  2. scanf and pointer
    By BlackSlash12 in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 03:33 PM
  3. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 09:22 AM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21