I'm trying to figure out how to get a string from an array starting at some given position. Say we have an array that's arbitrarily long and my string starts at location 1000. If I wanted to get a string from a file I would simply use something like getc or scanf or something. How do I carry out these same functions on an array instead of a file?

*oh, keep in mind that the array is of type int and is full of numerical representations of ASCII characters.

Recommended Answers

All 13 Replies

Depends on whether the strings in the array are null-terminated. If so, you can just construct the string using the (casted) starting address. If not, there's a constructor that takes a range.
When each int element only contains a single character, you'll have to convert the desired subarray first.

The strings are null terminated. What do you mean by using the casted starting address?

How I would carry out this operation if I knew how is:

string a;

while ( array(i) != NULL )
{
a = a + get_character(array(i));
i++;
}

There are many issues with this mostly having to do with how strings work and how ascii strings are stored.

For instance, how do I append the acquired characters to my string?

How do I index into the array? I assume i++ isn't sufficient because an acsii character takes 8 bytes of storage right?

How do I make the comparison of array(i) to NULL if array(i) is only one integer and null requires many bytes?

In otherward's... I can see generally what I want done, I just have no idea how to do it...

The strings are null terminated. What do you mean by using the casted starting address?

reinterpret_cast<char*>(array+1000) or reinterpret_cast<char*>(array)+1000, depending on whether your string is 1000 int elements into the array or 1000 char elements into the array.

For instance, how do I append the acquired characters to my string?

To append a character: a+=yourChar;

How do I index into the array? I assume i++ isn't sufficient because an acsii character takes 8 bytes of storage right?

No, it's 8 bits.

How do I make the comparison of array(i) to NULL if array(i) is only one integer and null requires many bytes?

Didn't get that.

So reinterpret converts my array from segment 1000 onwards to whatever type the data is being casted as? Where do I test for NULL? Ideally what I'd like to do is, starting at a specific location, take however many integer values make up one character, convert to ascii, test for null, then iterate until null is found, and exit the loop, all the while loading characters in my string. Is it possible to reinterpret a specified segment of data? Specifically one ascii character's worth.

Yes. although "1000" is relative. From the char perspective that would be position 4000.

I see, well, my array is mostly integer data peppered with the occasional file name. I'm handed a pointer to when my file name begins in my memory array.

what about using a stringstream and the getline() function with an obvious delimiter?

I see, well, my array is mostly integer data peppered with the occasional file name. I'm handed a pointer to when my file name begins in my memory array.

Oh, I nearly forgot, the data memory is comprised of several 8 bit HEX words. So when I'm given a pointer to where my filename resides, I'm given a pointer to 8 hex values which is 4 chars right? So what I'd like to do is decode this chunk into 4 chars, check in consecutive order to determine if one of them is a null and by the 4th one, if no NULL was found, goto the next array element and repeat. This seems like it would be easier to do.

Is there anything inherently wrong about doing this?:

//top->a0 is the pointer to the beginning of the filename
int i = 0;
int nullflag;
int step = top->a0;
string filename;
char temp[4] = reinterpret_cast<char*>( DataMemory(top->a0) );
while(temp[i]!=NULL)
{
     filename+=temp[i];//add non-NULL char to filename
     i++;//increment counter
     if(i==4)//if end of 4 element char array is reached without finding null then reset counter and start again on next element
     {
          i=0;//start over char array indexing
          step+=1;//move to the next parent array element
          temp[4] = reinterpret_cast<char*>( DataMemory(step) );//fill char array with the next 4 characters from parent array
     }
}

I get the following errors from this code:

../sim_main.cpp: In function ‘int main(int, char**)’:
../sim_main.cpp:237: error: no match for call to ‘(std::vector<int, std::allocator<int> >) (IData&)’ //LINE 6 IN THIS POST
../sim_main.cpp:238: warning: NULL used in arithmetic
../sim_main.cpp:246: error: no match for call to ‘(std::vector<int, std::allocator<int> >) (int&)’ //LINE 15 IN THIS POST
make: *** [sim_main.o] Error 1

temp should be a simple char pointer and line 15 should be just temp=...
If DataMemory is a vector, it should be DataMemory[step]. NULL should only be used (if at all) when you mean a null pointer - so the compiler warning is quite right.

The behaviour of your code is equivalent to the simpler variant:

string filename=reinterpret_cast<char*>(DataMemory[top->a0]);

In either case, this requires DataMemory to hold consecutive characters. If it holds one character per int element, it won't work. Nor will it work if the actual string you want to read is hex-encoded.

Perhaps I'm not thinking about this correctly. Here is a sample of what is inside of my array:

element 1000:14789343995
element 1001: 2296103839
etc...

So say I'm told my string starts at 1000.

Is it correct to think that 14789343995(int) = 371837AFB(hex) = (as an example even if the ascii conversion is incorrect)

and 2296103839 = 88DBC39F =

So does it matter that all of my memory elements are ints? Doesn't the recasting just take 14789343995(dec) and give me ?

Am I wrong to think a NULL exists as one of my characters? How do I test for the end of my filename if not with NULL?

Yes, if that's how it is, then the characters are stored consecutively in memory. So the method will work fine.

Am I wrong to think a NULL exists as one of my characters?

Not at all. However, you should use 0 instead of NULL, because you're dealing with characters here and not pointers, which is what the usage of NULL should be restricted to.

Everything compiles, I still need to test though. You mentioned using "string filename = reinterpret...". If I were to do this how would I then traverse the string? If I load the hex into my char array then I can easily just jump from one location to the next. Can I also do this with strings? Here is my code as of now just for reference:

int i = 0;
            int step = top->a0;
            string filename;
            char *temp = reinterpret_cast<char*>( DataMemory[top->a0] );
            while(temp[i]!=0)
            {
                filename+=temp[i];
                i++;
                if(i==3)
                {
                    i=0;
                    step+=1;
                    temp = reinterpret_cast<char*>( DataMemory[step] );
                }
            }

I'm getting a segfault again having to do with line 4. I get the feeling that using a string in this case would take care of it. I'm beginning to realize just how finicky character arrays are...

Okay, so, the problem seems to be my indexing of temp. I get a seg fault with the follwoing code:

int i = 0;
            //int step = top->a0;
            string filename;
            char *temp = reinterpret_cast<char*>( DataMemory[top->a0] );
            while(temp[i]!=0)
            {
                //filename+=temp[i];
                //i++;
               // if(i==3)
                //{
                    //i=0;
                    //step+=1;
                    //temp = reinterpret_cast<char*>( DataMemory[step] );
                //}
            }

Which leaves me to believe that either temp doesn't start at index 0 (sounds unlikely) or that I have to use some other operator (&?) to access temp. If I use the "&" (dereference operator?) what exactly will that mean in terms of my loops? I will be iterating through the elements in the array that temp points to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.