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

  • Home
  • SEARCH
  • 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 9180827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:08:45+00:00 2026-06-17T18:08:45+00:00

I have below code. In below code the value 5 gets stored in variable

  • 0

I have below code. In below code the value 5 gets stored in variable a(i.e abc.a). But I would like to intialize the value in c (i.e abc.c). How to do that? My requirement is to fill the data for c not for a.

typedef struct abc
{
    int a;
    int b;
    int c;
}abc;
typedef struct def{
    int *ptr;
    abc strpt;
}def;
typedef struct xyz{
int *pointer;
}xyz;
int main()
{
    int *temp, tmp;
    abc* ab;
    tmp = 5;
    temp = &tmp;
    ab = (abc*)malloc(sizeof(abc));
    xyz *x = (xyz*)malloc(sizeof(xyz));
    def *de = (def*)malloc(sizeof(def));
    x->pointer = (xyz*)temp;
    ab = (abc*)x->pointer;


    return 0;
}

Please help me.

  • 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-17T18:08:46+00:00Added an answer on June 17, 2026 at 6:08 pm

    I’m Affraid some serious lack of understanding pointers manifests in your code. Let me dissect it line by line to make clear where problems occur.

    I numbered the statements in order to make easy references, assuming the type declarations as above.

    int main()
    {
        int *temp, tmp;
        abc* ab;
        /* 1 */ tmp = 5;
        /* 2 */ temp = &tmp;
        /* 3 */ ab = (abc*)malloc(sizeof(abc));
        /* 4 */ xyz *x = (xyz*)malloc(sizeof(xyz));
        /* 5 */ def *de = (def*)malloc(sizeof(def));
        /* 6 */ x = (xyz*)temp;
        /* 7 */ ab = (abc*)x;
    
        return 0;
    }
    

    First of all: You never clean up the memory allocated in 3, 4 and 5. Even for a simple test program, you should always take care of stuff like this.

    Your first problem is line 6 where you blindly cast a pointer to an object of type int (4 bytes in memory) into a pointer to a structure of type xyz (4 or 8 bytes in memory, different type). What you do here is a complicated way to write x = (xyz*)&tmp;.

    An action like that leads to following serious problems:

    1. You just allocated memory in line 4. Now you overwrite this pointer with a pointer to your stack variable. Therefore you can’t free the allocated memory anymore ==> Memory leak
    2. Your newly assigned pointer to the stack variable becomes invalid after exiting the scope and can cause undefined behaviour.
    3. Any write attempt to elements of x after line 6 can lead to corruption of the stack (and therefore undefined behaivour) due to a possible size mismatch of elements.

    By line 7 you get the same problems again and even worse. The estimated sizeof(abc) is possibly 12 bytes. Accessing any element of abc after this line can lead to stack corruption again.

    I still don’t understand what you really are looking for but if you “have to” initialize a structure element through pointers there are a several ways:

    If the struct ist known you can do it like this:
    abc * ab = malloc(sizeof(abc));

    if (ab != NULL) {
        ab->c = 5;
    
        // -- do other stuff
    
        free(ab); ab = NULL;
    }
    

    If you need a pointer to the element ‘c’ you can do it like this:

    int * c_ptr = NULL;
    abc * ab = malloc(sizeof(abc));
    
    if (ab != NULL) {
        c_ptr = &(ab->c);
        *c_ptr = 5;
    
        //-- do other stuff
        free(ab); ab = NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below. I'm wondering that does the self.value and _value have
I have the below code: Sheet1.Range(AB3).Value = Application.WorksheetFunction.AverageIfs( _ .Range(AB5:AB & CStr(i)), _ .Range(AB5:AB
I have the code below, which sends the value of the textarea and either
I have this snippet of code below. I want to pass the value of
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have the following code below which works, but I want to insert values
I have a stored procedure that gets a list of items, sorts and applies
I have a requirement that should assign a counter variable for each thread that
I used the code below when initialising my sliders: //Gets the corresponding slider value,
The code below gets the values I have entered for my report parameters in

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.