-
December 2nd, 2009, 01:28 PM
#1
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;
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|