How to call/use function

This is a discussion on How to call/use function within the C Programming forums, part of the General Programming Boards category; Hi, I will set the pretext: Code: typedef struct { ubyte command; ubyte data_length; ubyte data[MAX_DATA_LENGTH]; WORD_UNION CRC; }COMMAND_PACKET; void ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    37

    How to call/use function

    Hi,

    I will set the pretext:

    Code:
    typedef struct
      {
      ubyte
        command;
      ubyte
        data_length;
      ubyte
        data[MAX_DATA_LENGTH];
      WORD_UNION
        CRC;
      }COMMAND_PACKET;
    
    void send_packet(COMMAND_PACKET *packet);
    How would I call/use the function send_packet() in the main()

    Can you give me an example of using it?

    Thanks

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Something like this:

    Code:
    int main()
    {
        COMMAND_PACKET package;
        package.command = some_value;
    
        send_packet(&package);
        return 0;
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    Thank you Ideswa. That worked a charm.

    But I have another question:

    Code:
    //============================================================================
    // 631_WinTest Code.
    // Copyright 2001-2004, Crystalfontz America, Inc. Written by Brent A. Crosby
    //
    // You may use this code in any project (including commercial) as long as
    // at least one of the intents of the project is to communicate with a
    // Crystalfontz display.
    //
    // http://www.crystalfontz.com brent@crystalfontz.com
    //============================================================================
    typedef unsigned char ubyte;
    typedef signed char sbyte;
    typedef unsigned short word;
    typedef unsigned long dword;
    typedef union
      {
      ubyte
        as_bytes[2];
      word
        as_word;
      } WORD_UNION;
    
    typedef struct
      {
      ubyte
        command;
      ubyte
        data_length;
      ubyte
        data[MAX_DATA_LENGTH];
      WORD_UNION
        CRC;
      }COMMAND_PACKET;
    I want to be able to write:

    Code:
    COMMAND_PACKET *packet;
    packet->CRC->as_word = //return of some function
    but it will not compile?

    Do you know how to fix this?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably because you CRC isn't a pointer? So you should use
    Code:
    packet->CRC.as_word = ...
    Edit: Also, your packet, as defined above, does not have any memory assigned to it, so CRC and any other members of the COMMAND_PACKET doesn't actually exist. Only a pointer to hold the address WHEN it gets created exists.

    --
    Mats
    Last edited by matsp; 04-17-2009 at 07:14 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21