Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:26:39+00:00 2026-06-09T11:26:39+00:00

In a header file, I declare a struct with an array and array length

  • 0

In a header file, I declare a struct with an array and array length like so:

typedef struct {
    unsigned char *frame;
    int length;
} ResponseFrame ;

extern ResponseFrame rf;

In my main file, I have the following:

ResponseFrame rf;

int main(void)
{
   while(1) {
      if (receive() == 0x01) {
         uint8_t val;
         rf.length = 6;
         for(int i = 0; i < 6; i++){
            val = receive();
            rf.frame[i] = val;
            transmit(val);            // LINE 1
         }


         for (uint8_t i=0; i<rf.length; i++){
            transmit(rf.frame[i]);    // LINE 2
         }
      }
   }
}

The array I’m receiving is

{ 00 00 FF 00 FF 00 }

The initial transmit responds with this received data [see LINE 1] .
However, when I try to transmit using the global variable rf.frame [see LINE 2], the first value is different like this —-

{ 13 00 FF 00 FF 00 }

Why is that first initial value different like this??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:26:40+00:00Added an answer on June 9, 2026 at 11:26 am

    You never allocate any memory for ResponseFrame.frame, so you’re running into undefined behaviour.

    You’re assuming that by doing rf.length = 6;, the size of unsigned char *frame; somehow magically increases, but it doesn’t.

    If you’re certain of the size, you need:

    rf.length = 6;
    rf.frame = malloc(sizeof(unsigned char) * 6);   //for C
    

    or use a std::vector in C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I declare my struct in a header file like so: typedef struct MyStruct{ int
I've declared a struct in my header file like this: typedef struct { NSString
can we declare a function in a header file in following way? extern int
Say I declare a header file with a variable: int count; Then in the
Hey. I have this in a header file: struct something { int a; int
gcc 4.4.4 c89 I have this in my header file. port.h struct struct_tag; int
I have the following C Header/Code Example: Header file struct category_info { int id;
I've created a structure in a header file as follows: typedef struct { GLfloat
Should all structs and classes be declared in the header file? If I declare
I have a header file variable.h where i declare all my global variable.Then i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.