Problems assigning structure members to pointers 
Author Message
 Problems assigning structure members to pointers

Hello,
I have been having problems assigning pointers to structure members.  I have
been able to assign a pointer to the first
member of  my structure but have not been able to assign a pointer to a
specific member.   Thanks a lot.

// structure definition
static struct symval {

char symbol[MAX_SYMBOL_LEN];
double value;

Quote:
};

// declared array of structures and parallel pointer

static struct symval current[MAX_NUM_SYMBOLS];
static struct symval *ptrstr[MAX_NUM_SYMBOLS];

// assignments: The first assignment works fine.  I think this is because I
assigning the first member and do not have to
// worry about the member syntax.  In the second assignment I have tried to
add the ampersand, but I get a compile error
// "incompatible types".  Without the ampersand I don't get any errors but I
don't get the assignment I want either.  I am at
// my wits end!  Please help.

      ptrstr[middle] = &current[num_symbols];
      ptrstr[middle]->value = current[num_symbols].value;



Wed, 26 May 2004 07:02:18 GMT  
 Problems assigning structure members to pointers

Quote:

> Hello,
> I have been having problems assigning pointers to structure members.  I have
> been able to assign a pointer to the first
> member of  my structure but have not been able to assign a pointer to a
> specific member.   Thanks a lot.

> // structure definition
> static struct symval {

> char symbol[MAX_SYMBOL_LEN];
> double value;

> };

> // declared array of structures and parallel pointer

> static struct symval current[MAX_NUM_SYMBOLS];
> static struct symval *ptrstr[MAX_NUM_SYMBOLS];

> // assignments: The first assignment works fine.  I think this is because I
> assigning the first member and do not have to
> // worry about the member syntax.  In the second assignment I have tried to
> add the ampersand, but I get a compile error
> // "incompatible types".  Without the ampersand I don't get any errors but I
> don't get the assignment I want either.  I am at
> // my wits end!  Please help.

>       ptrstr[middle] = &current[num_symbols];
>       ptrstr[middle]->value = current[num_symbols].value;

Hillary...

ptrstr[] is an array of pointers to symval{}.

The first assignment is OK because you are assigning an address
of (pointer to) a symval{}, as advertized.

In the second assignment ptrstr[middle] (set by the preceeding
assignment) is used to reference the value element of that
structure (using ptrstr[middle]->value) and you can, indeed,
store the value element from another symval{} into it.

It doesn't make sense to attempt:

   ptrstr[middle]->value = &current[num_symbols].value;

because you would be attempting to store a *pointer to a double*
into a symval{} element that you promised to put only *double*
into!

--
Morris Dovey
West Des Moines, Iowa USA



Wed, 26 May 2004 06:18:41 GMT  
 Problems assigning structure members to pointers


Quote:
>Hello,
>I have been having problems assigning pointers to structure members.  I have
>been able to assign a pointer to the first
>member of  my structure but have not been able to assign a pointer to a
>specific member.   Thanks a lot.

>// structure definition
>static struct symval {

static isn't needed here.  Code outside this compilation unit won't
see the structure definition.  static is only relevant to the
visibility of variables not the visibility of variable types.

Quote:

>char symbol[MAX_SYMBOL_LEN];
>double value;

>};

>// declared array of structures and parallel pointer

>static struct symval current[MAX_NUM_SYMBOLS];
>static struct symval *ptrstr[MAX_NUM_SYMBOLS];

>// assignments: The first assignment works fine.  I think this is because I
>assigning the first member and do not have to
>// worry about the member syntax.  In the second assignment I have tried to
>add the ampersand, but I get a compile error
>// "incompatible types".  Without the ampersand I don't get any errors but I
>don't get the assignment I want either.

It's incorrect to add the ampersand and does, indeed, result in an
incompatible type assignment.  ptrstr[middle]->value is a double, so
you want to assign it a double.  Placing the & before the rvalue makes
the assignment a pointer to double.

Quote:
> I am at
>// my wits end!  Please help.

>      ptrstr[middle] = &current[num_symbols];
>      ptrstr[middle]->value = current[num_symbols].value;

What you've provided looks correct.  For instance, this code works:

#include <stdio.h>
#include <string.h>

struct symval {
   char symbol[10];
   double value;

Quote:
};

static struct symval current[10];
static struct symval *ptrstr[10];

int main(void)
{
   strcpy(current[0].symbol,"hello");
   current[0].value=5.5;  

   ptrstr[0] = &current[0];
   ptrstr[0]->value=10.5;

   printf("%s; %f\n",ptrstr[0]->symbol,ptrstr[0]->value);
   return 0;

Quote:
}

Output:

hello; 10.5

It would be nice to see the actual code you're using as I suspect the
problem is hidden there.  Try putting together the smallest
*compilable* piece of code that reproduces your problem.

Russ
--

Replace all the N's with i's to reply.



Wed, 26 May 2004 07:34:16 GMT  
 Problems assigning structure members to pointers

Quote:

> Hello,
> I have been having problems assigning pointers to structure members.  I have
> been able to assign a pointer to the first
> member of  my structure but have not been able to assign a pointer to a
> specific member.   Thanks a lot.

> // structure definition
> static struct symval {

> char symbol[MAX_SYMBOL_LEN];
> double value;

> };

> // declared array of structures and parallel pointer

> static struct symval current[MAX_NUM_SYMBOLS];
> static struct symval *ptrstr[MAX_NUM_SYMBOLS];

> // assignments: The first assignment works fine.  I think this is because I
> assigning the first member and do not have to
> // worry about the member syntax.  In the second assignment I have tried to
> add the ampersand, but I get a compile error
> // "incompatible types".  Without the ampersand I don't get any errors but I
> don't get the assignment I want either.  I am at
> // my wits end!  Please help.

>       ptrstr[middle] = &current[num_symbols];
>       ptrstr[middle]->value = current[num_symbols].value;

This looks legitimate but pointless.
What were you expecting?
After
    ptrstr[middle] = &current[num_symbols];
ptrstr[middle]->value   and    current[num_symbols].value
refer to the same location so you are copying a structure member
back into itself. You can't really see that the assignment has
occured and a very smart compiler is in any case allowed to skip
the redundant operation!

Malcolm Kay



Wed, 26 May 2004 10:08:25 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Assigning structure pointer to function pointer

2. Pointer to members of structure

3. Pointer to members of structure

4. Array of pointers to structure members

5. pointer to function as a member of a structure

6. Pointer to function as structure member

7. Getting a pointer to a structure from its members

8. Accessing members of a structure array through a pointer

9. Problem assigning function to function pointer

10. Pointers assigned to pointers - confused

11. Language Suggestion: Default member for single-member structures

12. Referring to structure member inside structure

 

 
Powered by phpBB® Forum Software