Solved

STRUCT as a Parameter

Posted on 2003-11-16
9
832 Views
Last Modified: 2013-12-14
In my header file I have the following structure delared:

typedef struct
{
      unsigned long ProtocolID;
      unsigned char Data[4128];
} PASSTHRU_MSG;

Then in my code I have the following function
DLLIMPORT long _stdcall PassThruReadMsgs
(
       PASSTHRU_MSG *pMsg,
      unsigned long *pNumMsgs
)
{
    *pNumMsgs = 23;
    pMsg.DataSize = 1;  
}

I get the following error when I try to compile
87 C:\Dev-Cpp\AUTSCN32\autscn32.c
request for member `DataSize' in something not a structure or union
0
Comment
Question by:matthewdheilman
  • 2
  • 2
  • 2
  • +1
9 Comments
 

Accepted Solution

by:
simbo earned 63 total points
ID: 9760607
First off, you are trying to dereference a pointer. So your code should be have the dereference symbol "->" instead of ".".

Secondly you don't have DataSize defined for the PASSTHRU_MSG structure.

Hope this helps!

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9762499
In C, Structure members can be accessed using either of two operators - the dot (.) OR the Arrow (->)

When u have an object/variable of type of your structure, u can access the structire member variables using the '.' operator

When u have a pointer to an object/variable of type of your structure, u can access the structire member variables using the '->' operator


Example

typedef struct
{
     unsigned long ProtocolID;
     unsigned char Data[4128];
} PASSTHRU_MSG;

int f ( ) {
       PASSTHRU_MSG   msg;
       PASSTHRU_MSG   *ptr_msg;
       msg.ProtocolID = 2 ;
     
       ptr_msg  = &msg;
       ptr_msg  -> ProtocolID = 2 ;
}

In your case, you are recieving a pointer to structire in your function as parameter, thus u should use the member deference '->' operator
Also, There is no member with the name of DataSize in your structure


HTH
Amit
0
 

Author Comment

by:matthewdheilman
ID: 9768332
You are correct, I didn't mean datasize, I meant Data.

In the above example I want to do something like:
ptr_msg -> ProtocolId = 2;
ptr_msg -> Data = "Test";

The compiler doesn't seem to like the ptr_msg -> Data = "Test", but using the -> for ProtocalID worked.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Expert Comment

by:simbo
ID: 9768594
If you are trying to copy a string use the strcpy function (make sure you end your string with null ) ....

eg:
  strcpy(ptr_msg->Data,"Test\0"); .....

The compiler gives you an error because you are assigning the string to a pointer (that is not allowed). You can however assign the string to the memory location pointed to by the pointer. So the easiest way is to use strcpy and you must end your string with a null (\0) if not you will overrun and write to memory location you are not suppose to.
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 62 total points
ID: 9768896
In C, whenever u want to copy a char array to a another char array, u cannot use the assignemnt operator '='.

C provides a set of functions to u to achieve this

The first set includes functions like memcpy() and strcpy(). have a look at the help of these functions

But, in the above functions, u need to provide a NULL character to be appnended for the destination char array

The second set of functions are the sprintf (), snprintf(), asprintf(). There are very handy functions for string copying.

Have a look at help of all these functions and decide which one is suitable for u. Post your queries (if any)
HTH

Amit

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9774069
simbo, check out the following:

--------8<--------
#include <iostream>

int main()
{
    char test[] = "Test";
    std::cout << "sizeof(\"" << test << "\") is " << sizeof(test) << '\n';
    for (int i = 0;i < sizeof(test);i++)
        std::cout << "Character " << (i+1) << " is 0x" << std::hex << static_cast<int>(test[i]) << '\n';
}
--------8<--------

String literals automatically get a '\0' terminator, which is why the sizeof(test) is 5.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9774101
Oops, forgot this was the C TA. Try this, simba...

--------8<--------
#include <stdio.h>

int main()
{
    char test[] = "Test";
    int i;

    printf("sizeof(\"%s\") is %u\n",test,sizeof(test));
    for (i = 0;i < sizeof(test);i++)
        printf("Character %d is 0x%x\n",i+1,test[i]);
}
--------8<--------
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now