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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:51:08+00:00 2026-05-24T22:51:08+00:00

everyone. Here is a test program I wrote. #include <stdio.h> #include <stdlib.h> typedef struct

  • 0

everyone.

Here is a test program I wrote.

#include <stdio.h>
#include <stdlib.h>

typedef struct _item {
    int value;
} item , *pItem;

typedef struct _itemcontainer {
    pItem i;
} itemcontainer, *pItemcontainer;

int main(void) {
    item i;
    i.value = 1;

    pItem pi = &i;
    pItem* ppi = &pi;

    itemcontainer ei = {&i};
    pItem* pei = (pItem*)(&ei);

    pItem api[1] = {&i};

    printf("First case: %d\n", (*ppi)->value);
    printf("Second case: %d\n", (*pei)->value);
    printf("Third case: %d\n", (*api)->value);


    return EXIT_SUCCESS;
}

The three printf functions’ results are the same value, i.e. 1. From the code, the initialization of variable ei and api are both {&i}. So I guess pItem* pei = (pItem*)ei should work, however it failed. Could any one tell me the difference between ei and api? It seems to be related with how the compiler deal with struct and array, which I am not good at. I need a concrete explanation. Thanks in advance.

Best Wishes

Jfhu

  • 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-05-24T22:51:09+00:00Added an answer on May 24, 2026 at 10:51 pm

    The difference is that the array name is implicitly converted to a pointer to its first element in most situations, while the name of an object of struct type is not.

    The expression pItem* pei = (pItem*)api; would compile because it is equivalent to pItem* pei = (pItem*)(&api[0]);

    The expression pItem* pei = (pItem*)ei; would not compile because it attempts to cast an object of struct type (a value) to a pointer to some unrelated type.

    The expression you used, pItem* pei = (pItem*)(&ei); creates a pointer to the struct object and reinterprets it to mean pointer to pItem. Since pItem is the first element of the struct, it is located at the same memory address as ei, and this works. (this is even guaranteed by the standard, §6.7.2.1/13: “pointer to a structure object, suitably converted, points to its initial member”)

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

Sidebar

Related Questions

Hello everyone here is my DAO class : public class UsersDAO extends HibernateDaoSupport {
I think everyone here would agree that in order to be considered a professional
UPDATE: So pretty much everyone here has told me that I just need to
i hope that everyone here know the php 'variable variable' syntax: $color = 'red';
Ahoy hoy everyone :) Here is a list of links. You will need it
I'm trying to create a web application project template for everyone to use here
First question here so hello everyone. The requirement I'm working on is a small
Hey everyone, here is the site in question: http://www.myvintagesecret.com/ I have a bunch of
hey everyone, here is the site SEE BELOW I have a slight jquery problem
Hey everyone, great community you got here. I'm an Electrical Engineer doing some programming

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.