Thread: how to scanf a global variable outside the main ?

  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,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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, 01: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