Like Tree3Likes
  • 1 Post By grumpy
  • 2 Post By Salem

how to scanf a global variable outside the main ?

This is a discussion on how to scanf a global variable outside the main ? within the C Programming forums, part of the General Programming Boards category; Hello the problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    7

    how to scanf a global variable outside the main ?

    Hello
    the problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .
    look at the code
    so what can I do to solve this problem ?
    Code:
    int nbEdges,nbVetices, nbColours ; typedef struct st_graphVertex { int index; int colour; int val ; int vertexDegree[nbColours]; // it won't work because nbColours unknown // here and I want get it from file in the main struct st_graphVertex *next; t_edgeList *out; }t_grapheVertex;
    Last edited by Islam Assi; 09-07-2013 at 03:20 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    Make vertexDegree a pointer, and use malloc() to allocate memory sufficient for an array of nbColours ints. Preferably store nbColours as a member of the struct, so you have a record of the allocated size.

    Beyond that, it is a case of writing actual code for a main() function (!), writing code to read required values.
    Islam Assi likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Well you start with this
    Code:
    typedef struct st_graphVertex 
    {
        int index;
        int colour;
        int val ;
        int *vertexDegree;
       struct st_graphVertex *next; 
        t_edgeList *out;
    }t_grapheVertex;
    Then when you do know what is stored in nbColours, you can do this.
    Code:
    foo->vertexDegree = malloc( nbColours * sizeof(*foo->vertexDegree) );
    Then you can access foo->vertexDegree[0] etc up to the limit you've set.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    7
    thank you Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 12:00 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  5. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM

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