CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    21

    Assign value to element in dynamic array, turns out to be NULL

    Hello,

    I am developing code to

    1. read the number of lines in a file ipFile, and store that value in ipLength
    2. create a dynamic array ipArray of size ipLength
    3. initialize ipArray
    4. reposition the file pointer to the beginning of ipFile
    5. get one line from ipFile, store it into ipAddress
    6. assign ipAddress to an element of dynamic array ipArray

    For debugging purposes, I want to display the contents of each element of the dynamic array. When i printf ipAddress, it is fine, however when I printf &ipArray[p], it turns up blank for each element. If I printf ipArray[p], it displays <null> for each element.

    Below is my code, please guide:

    int ipLength=0;

    while (!ipFile.eof())

    {//begin while !ipFile.eof()

    getline (ipFile,ipLine);
    ipLength++;

    }//end while !ipFile.eof()


    string *ipArray = NULL;
    ipArray = new string [ipLength];

    for (int i=0; i<ipLength; i++)

    {//begin for loop

    ipArray[i] = "";

    }//end for loop

    ipFile.seekg(0, ios::beg);
    int p=0;

    while (!ipFile.eof())

    {//begin while !ipFile.eof()

    getline (ipFile,ipLine);
    int TempNumOne=ipLine.size();
    char ipOutput[100]={0};

    for (int a=0;a<=TempNumOne;a++)

    {//begin for loop

    ipOutput[a]=ipLine[a];

    }//end for loop

    sscanf(ipOutput, "%s", &ipAddress);
    ipArray [p] = ipAddress;
    printf ("%d. %s\n", p, ipArray[p]);
    p++;

    }//end while !ipFile.eof()


    ipFile.close();
    delete [] ipArray;
    ipArray = NULL;

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Assign value to element in dynamic array, turns out to be NULL

    The type of ipArray[p] is a C++ std::string. Printf is a C function, and does not understand C++ classes; it expects the argument corresponding to a &#37;s to be a const char*.

    Try outputting ipArray[p].c_str(), or else do your output with cout rather than printf.

    Also, I would recommend using a std::vector<std::string> rather than a dynamic array.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Assign value to element in dynamic array, turns out to be NULL

    In the first section, you read until end of file. Once this happens, the internal
    state of the stream is such that trying to use it further will fail. You need
    to clear the state.

    So, before doing the seekg() ...

    Code:
    ipFile.clear();
    Also, I agree with previous post ... use a vector<string> and you can do everything
    in one pass thru the file.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Assign value to element in dynamic array, turns out to be NULL

    What do noobs have against white space? Seriously, get in the habit of putting spaces between variables and operators and anywhere else you can to make your code readable.

  5. #5
    Join Date
    Dec 2009
    Posts
    21

    [SOLVED] Assign value to element in dynamic array, turns out to be NULL

    Quote Originally Posted by Lindley View Post
    The type of ipArray[p] is a C++ std::string. Printf is a C function, and does not understand C++ classes; it expects the argument corresponding to a %s to be a const char*.

    Try outputting ipArray[p].c_str(), or else do your output with cout rather than printf.

    Also, I would recommend using a std::vector<std::string> rather than a dynamic array.
    Awesome, it now works, thanks!

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Assign value to element in dynamic array, turns out to be NULL

    Quote Originally Posted by GCDEF View Post
    What do noobs have against white space? Seriously, get in the habit of putting spaces between variables and operators and anywhere else you can to make your code readable.
    White space takes up space! Both on your display and on your disk!

    Viggy

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured