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 8120851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:17:11+00:00 2026-06-06T05:17:11+00:00

I got a struct and an initializing function like following: struct someStruct { int*

  • 0

I got a struct and an initializing function like following:

struct someStruct {
    int* b, r;  // r is a pointer to an integer in b
};

someStruct* init_struct(size_t size) {
    someStruct* p = (someStruct*) malloc(sizeof(someStruct));
    p->b = (int*) malloc(size);
    p->b[0] = 16;
    p->r = &p->b;  // Here's the error, why?
    return p;
}
  • 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-06T05:17:14+00:00Added an answer on June 6, 2026 at 5:17 am

    In your code the r field is an integer, not a pointer to an integer ! Also, &(p->b) is a pointer to a pointer to an integer (int**). You need to define your struct like this if you want r to be pointing a integer in the b array :

    struct someStruct {
        int *b, *r;
    }
    

    And assign the r pointer by dereferencing an element of b (int), not the pointer itself (int*):

    p->r = &(p->b[0]);
    

    Or don’t dereference b at all if you want to point to b[0] :

    p->r = p->b;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got something like: typedef struct Data_s { int field1; int field2; } Data;
I've got a following struct struct teststruct { int *a; void *data; }; Is
I got a struct for client and server like this: { char type; int
I' ve got such a function static ssize_t read_mydevice(struct file *filp, char* buf, size_t
I got a simple struct private struct _config { public string name; public int
I have got an array of the following structs: typedef struct _my_data_ { unsigned
I've got a struct defined in my GLSL like so: struct Rect { float
I've got the following structs in C++: (Using pragma pack 1) typedef struct _wfs_cdm_physicalcu
I've got the following structure: struct A { A(); virtual ~A(); virtual void Foo()
I've got this sample code: struct A { int foo() { return 27; }

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.