initialize array based on condition.

This is a discussion on initialize array based on condition. within the C++ Programming forums, part of the General Programming Boards category; I want to declare an array based on the condition (board type). I get the board type as return value ...

  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,799
    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
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    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,&dwBytesWritten);

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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,027
    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
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    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,&dwBytesWritten);

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, 02: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-26-2006, 11:56 PM
  3. initialize 2D array!
    By samirself in forum C Programming
    Replies: 1
    Last Post: 03-10-2005, 10: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, 03:28 PM

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