Solved

STRUCT as a Parameter

Posted on 2003-11-16
9
835 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

911 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

19 Experts available now in Live!

Get 1:1 Help Now