Thread: initialize array based on condition.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    initialize array based on condition.

    I want to declare an array based on the condition (board type). I get the board type as return value from a function. Have written the snippet of the code from my project below.

    Code:
    #include <stdio.h>
    
    #define F120 1
    #define F120EX 2
    
     int getPlatformType();
    int main()
    {
    
        int platformType;
    
        platformType = getPlatformType();
    
    
        if (platformType == F120) {
            const char *portList[] = {"eth0", "eth1" };
        } else if (platformType == F120EX ) {
            const char *portList[] = {"eth0", "eth1", "eth3", "eth4"};
    
        }
    
       int portsize = sizeof(portList) / sizeof(portList[0]);
    
    // other code follows..
        return 0;
    }
    
    
    /* Test purpose only */
    int getPlatformType()
    {
    
        return F120;
    
    
    }

    I am getting the following compilation error :


    g++ portdefine.cpp
    portdefine.cpp: In function 'int main()':
    portdefine.cpp:23: error: 'portList' was not declared in this scope[/B]


    Please let me know the correct method
    Last edited by cjjoy1980; 04-23-2012 at 11:31 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I'd suggest using a std::vector of std::string objects.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    I concur. I'm in the process now of trying to stay with one language and one only. When I was first picking up C++, I had a mix of things going on using printf instead of iostream and almost never using string and stringstreams, etc. The more STL containers I use and the more C++ methods I use, the happier I am becoming with my code and the easier it is becoming to read it. I will never say that I am good at C because I am awful at it, but I can say that I love C++ and can do it with pride although not necessarily ease.

    Also you could make them non-constant and just declare it before, initializing it in your conditions.

    Also, if you don't know, the reason that you are receiving that error is because those const variables only have scope for those if statements. Once those statements pass, those char* arrays no longer exist, and therefore cannot be referenced. You have to declare them before, or better, use an STL container such as std::vector mentioned above.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could also try to get this to work at compile time with macros, I think, but it depends on what getPlatform is really supposed to do.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Few things:
    1) Prefer stl containers like vector to arrays. Here the size of the container will depend on a runtime condition, so a vector is the perfect choice.
    2) In this case it's probably easier to default initialize the array/containers, and then change them based on the condition. This is easy with std::vector.
    3) In other cases where you need to initialize based on a runtime condition, the way to do it is to have a "Factory" that will build the object you want, and return it so you can initialize your local object. That's overkill here.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    array - C++ Reference

    *angelic choir*
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array in if condition.
    By mooshty in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 03:50 PM
  2. pull out data frm a text file based on a condition
    By nirmala.s in forum C Programming
    Replies: 21
    Last Post: 11-27-2006, 12:56 AM
  3. initialize 2D array!
    By samirself in forum C Programming
    Replies: 1
    Last Post: 03-10-2005, 11:56 AM
  4. how to initialize a new array
    By Chiel in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2003, 01:30 PM
  5. Initialize an array
    By cheesehead in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2001, 04:28 PM