Solved

STRUCT as a Parameter

Posted on 2003-11-16
9
856 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 and create new code templates in NetBeans IDE 8.0 for Windows.

630 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