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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:45:42+00:00 2026-06-06T12:45:42+00:00

I have this code which does the trick: #include <stdio.h> int main() { int

  • 0

I have this code which does the trick:

#include <stdio.h>
int main()
{
    int a = 30000, b = 20,sum;
    char *p;
    p=(char *)a;
    sum = (int)&p[b]; // adding a & b
    printf("%d",sum);
    return 0;
}

Can someone please explain what is happening in the code?

p = (char*)a;
sum = (int)&p[b]; // adding a & b
  • 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-06T12:45:44+00:00Added an answer on June 6, 2026 at 12:45 pm

    I think it is worth adding to the other answers a quick explanation of pointers, arrays and memory locations in c.

    Firstly arrays in c are just a block of memory big enough to hold the number of items in the array (see http://www.cplusplus.com/doc/tutorial/arrays/)

    so if we said

    int[5] example;
    example[0] = 1;
    example[1] = 2;
    example[2] = 3;
    example[3] = 4;
    example[4] = 5;
    

    Assuming int is 32 bits we would have a block of memory 5*32bits = 160bits long.
    As C is a low level language it tries to be as efficient as possible, therefor stores the least amount of information about arrays as possible, in this case the least amount possible is the memory address of the first element. So the type of example could be expressed as

    int *example;
    

    Or example points to an int. To get the items in the array you then add the correct number to the address stored in example and read the number at that memory address.
    If we assumed memory look like

    Memory Address = Value (ints take up 4 bytes of space)
              1000 = 1          <-- example
              1004 = 2   
              1008 = 3   
              1012 = 4   
              1016 = 5
    

    So

    int i = example[3];  //The 4th element
    

    could be expressed as

    int i = *(example + 3 * sizeof(int));
    int i = *(example + 3 * 4);
    int i = *(1000 + 12);
    int i = *(1012); // Fetch the value at memory location 1012
    int i = 4;
    

    The sizeof(int) is 4 (int is 32 bits, or 4 * 8 bit bytes). If you where trying to do addition you would want a char which is 8 bits or 1 * 8 bit bytes.

    So back to you code

    char* p;       // declare p as a pointer to a char/
    p = (char *)a; // point p at memory location 3000
    // p[b] would be the 21st element of the "array" p =>
    // p[20]  =>
    // p + 20 * sizeof(char) =>
    // p + 20 * 1 =>
    // p + 20  =>
    // 3000 + 20 =>
    // 3020
    // the & operator in c gets the address of the variable so
    sum = (int) &p[b]; 
    // &p[b] => find the address pointed to by p[b] => 3020
    // (int) casts this pointer to a int.
    

    So sum is assigned the address of the 21st element of the array.

    Long winded explanation.

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

Sidebar

Related Questions

On my main page I have this jquery code which does an ajax call
I have this method which does certain checks and applies a color code on
I have this code which will include template.php file from inside each of these
I have this code which searches a string array and returns the result if
I have this code which iterates through ArrayList<String> towns = new ArrayList<String>(); using JSTL
I have this code which I use on my swing application: ReportClientDocument rpt =
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which compiles and works as expected: class Right {}; class

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.