Solved

STRUCT as a Parameter

Posted on 2003-11-16
9
844 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to save 10 174
Eclipse. How to move up/down source folders 4 103
delete-remove 14 107
Visual Studio 2013 and Eclipse Neon 6 67
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

679 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