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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:12:45+00:00 2026-05-27T23:12:45+00:00

When I run this code: int arr[3] = {2,3,4}; char *p; p = (char*)arr;

  • 0

When I run this code:

int arr[3] = {2,3,4};
char *p;
p = (char*)arr;
printf("%d", *p);
p = p+1;
printf("%d", *p);

The output is 2 and 0. The second result is slightly confusing. Could someone explain why this is happening?

  • 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-27T23:12:46+00:00Added an answer on May 27, 2026 at 11:12 pm

    Let’s break this down:

    int arr[3] = {2,3,4};
    

    Creates an array of 3 integers. Assuming your system is 32bit little endian this is how it looks in memory:

    02 00 00 00 03 00 00 00 04 00 00 00
    
    char *p; p = (char*)arr;
    

    p now points to arr but is a pointer to char*. In other words, p points to the 02.

    printf("%d", *p);
    

    You are printing as an int the location referenced by p. So when you dereference p (by writing *p) you are accessing the char (since p is of type char*) referenced by p. Which is 02.

    p = p+1;
    

    p now points to the 00 just after 02, because p is char*. So when you add 1, it will move by 1 * sizeof(char) = 1 * 1 = 1 byte in memory.

    printf("%d", *p);
    

    You are printing as an int the location referenced by p. So when you dereference p (by writing *p) you are accessing the char (since p is of type char*) referenced by p. Which is 00.

    If you wanted to print 3 instead of 0 you have to change your pointer type to int* instead of char*, making the pointer move by 1 * sizeof(int) = 1 * 4 = 4 bytes in memory.

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

Sidebar

Related Questions

when i run this code : CONADefinitions.CONAPI_FOLDER_INFO2 FolderInfo; int iResult = 0; IntPtr Buffer
Trying to convert this c code into MIPS and run it in SPIM. int
When I run this code url = ('http://maps.google.com/maps/nav?'+ 'q=from%3A'+from_address+ '+to%3A'+to_address+ '&output=json&oe=utf8&key='+api_key) request = urllib2.Request(url)
Why run this code and print whole string? #include <stdio.h> void main() { int
When I run this code: #include <stdio.h> typedef struct _Food { char name [128];
Consider this code: class Jm44 { public static void main(String args[]){ int []arr =
I run this code: graphic.addAnimation("standart",new int[] {0,1},1.0,true); which calls the graphic.addAnimation(String,int[],float,boolean) Method: public void
I am trying to run this code in android using eclips IDE. int maxrow=0;
I want to run this piece of code Bitmap grayImage = (Bitmap)img.Clone(); for (int
When I run this code the program freezes,why? main() { int co; co=0; while

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.