Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 872
  • Last Modified:

STRUCT as a Parameter

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
matthewdheilman
Asked:
matthewdheilman
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
simboCommented:
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
 
Sys_ProgCommented:
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
 
matthewdheilmanAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
simboCommented:
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
 
Sys_ProgCommented:
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
 
rstaveleyCommented:
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
 
rstaveleyCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now