pointers to structs

Atomical
Unregistered
 
Post: #1
When this code executes it copies the last letter into the string. So if it processed abcdefghi and I called cout << *parsed[headercount].data; it would return i. It doesn't seem to be accessing the pointer data. How do I do that?


Code:
struct header_information{
    nt id;
    char* data;
};

//parsed is of type header_information and it is a pointer
bracketpos1 += 2;
z = 0;
parsed[headercount].data = (char *)calloc(9000,sizeof(char)*9000);
while ( z < datalen ) {
    strcpy(parsed[headercount].data, &header[bracketpos1+z]);
    z++;
}
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #2
You might want to add the entire code, like what is header? What are you TRYING to do with it? And if you are using C++, might want to use the new operator instead of calloc/malloc/etc. If anything, it makes it look better.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #3
Not sure what exactly are you trying to do, but that calloc call seems wrong, shouldn't it be just calloc(9000,sizeof(char))? Check calloc description here.

Also, you use of strcpy seems wrong, check strcpy description here.
Quote this message in a reply
Atomical
Unregistered
 
Post: #4
I want to put a char from header[] into parsed[].
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #5
Ok, then I see what you are doing now. strcpy copies a string into another string. If you copy each character(which is what you are currently doing), the last copy holds. That is why you only get the last character.

I don't really know what data type header is. I will assume it is a string.

This is all the code you need:
Code:
parsed[headercount].data = new char[strlen(header)];
strcpy(parsed[headercount].data, header);
Quote this message in a reply
Atomical
Unregistered
 
Post: #6
I don't want to insert the whole header, just some of it.. That's what brackpos1+z is for. I thought because data is of type char * that I could create memory and add to it. I want strcpy to take the character from header and add it to data.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #7
Atomical Wrote:I don't want to insert the whole header, just some of it.. That's what brackpos1+z is for. I thought because data is of type char * that I could create memory and add to it. I want strcpy to take the character from header and add it to data.

Sure, but you are always copying to the same exact place, "parsed[headercount].data".

Get rid of the while loop, and simply use memcpy. Wink
(or strncpy, also without the while loop)
Quote this message in a reply
Atomical
Unregistered
 
Post: #8
Doesn't that assume that what I want is at the beginning of the string? Don't I need to use substr or something?

EDIT: I googled substr and found this function.

int substr(char* str, char* substr, int offset, int length)
{
int s_length = strlen(str);

if ((str != NULL) && (length > 0))
{
if (offset >= 0)
{
if ((length - offset) >= s_length)
{
length = s_length - offset;
}
}
else
{
if ((length - (s_length + offset)) >= (s_length + offset))
{
length = s_length - (s_length + offset);
}
offset = s_length + offset;
}
if ((offset >= 0) && ((s_length - 1) >= offset) &&
(s_length >= (offset + length)))
{
strncpy(substr, str + offset, length);
substr[length] = '\0';
return(TRUE);
}
else if ((offset < 0) && (s_length >= (offset * -1)) &&
(s_length >= ((s_length + offset) + length)))
{
strncpy(substr, str + (s_length + offset), length);
substr[length] = '\0';
return(TRUE);
}
}
else if ((str != NULL) && (length < 0))
{
if (offset < 0)
{
offset = s_length + offset;
}
if ((offset >= 0) && ((s_length - 1) >= offset) &&
(s_length > (length * -1)) &&
((s_length + length) > offset) &&
(s_length >= ((s_length + length) - offset)))
{
strncpy(substr, str + offset, ((s_length + length) - offset));
substr[((s_length + length) - offset)] = '\0';
return(TRUE);
}
}

substr[0] = '\0';
return(FALSE);
}
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #9
Well, that is an option, but wouldn't this be easier?:

strncpy(parsed[headercount].data, &header[bracketpos1], datalen);

(assuming of course that your original string is null terminated - otherwise, just add parsed[headercount].data[datalen] = 0; below)

Or, if you are using C++ use the standard substr method.
Quote this message in a reply
Atomical
Unregistered
 
Post: #10
I'm not sure if you're not understanding me or if I'm not understanding you. I don't want header to start at zero and continue to length z. I want it to start at bracketpos1 and continue to z.

Quote:char * strncpy ( char * dest, const char * src, sizet_t num );

Copy characters from one string to another.
Quote this message in a reply
Atomical
Unregistered
 
Post: #11
I might as well just tell you what I'm trying to do because I'm doing it wrong I think.

I'm trying to parse a file like this:

[id][length][string]

where length is the length of the string. So I'm trying to load the id and the string into the struct.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #12
Atomical Wrote:I'm not sure if you're not understanding me or if I'm not understanding you. I don't want header to start at zero and continue to length z. I want it to start at bracketpos1 and continue to z.
Quote:char * strncpy ( char * dest, const char * src, sizet_t num );

Copy characters from one string to another.

In your original post, you had:
strcpy(parsed[headercount].data, &header[bracketpos1+z])

So I thought you understood what "&header[bracketpos1+z]" meant.
If you look at the code I posted previously, you'll see that I used &header[bracketpos1], that means "the substring of header sarting at position bracketpos1". So strncpy would do what you want. Smile
Quote this message in a reply
Atomical
Unregistered
 
Post: #13
What's generally the best way to parse through a file? I wrote my own function called stripos to find the first occurance of the string. Is there a better way?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #14
Quote:STRSTR(3) BSD Library Functions Manual STRSTR(3)

NAME
strstr, strcasestr, strnstr, strcasestr_l -- locate a substring in a
string
man strstr!

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  c pointers and memory allocation NelsonMandella 15 7,393 Feb 27, 2010 12:59 AM
Last Post: Kerome
  C++ pointers (multiple uses inside a class) problems :( wyrmmage 14 5,891 Jun 2, 2007 10:08 AM
Last Post: akb825
  Casting Structs BeyondCloister 2 2,461 Nov 29, 2006 12:48 PM
Last Post: OneSadCookie
  trouble with structure pointers ferum 2 2,875 Mar 24, 2006 05:57 PM
Last Post: ferum
  Problems With Arrays of Pointers Nick 8 3,873 Sep 27, 2005 04:53 PM
Last Post: Zekaric