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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:36:32+00:00 2026-05-27T04:36:32+00:00

my effort to understand pointers better, I wrote this code: int *a = 17;

  • 0

my effort to understand pointers better, I wrote this code:

int *a = 17;

printf("%d", a+3);

It compiled fine under c using gcc-4.3.4: http://ideone.com/abotd

And yet it failed to compile with c++: http://ideone.com/IdGHy

I would like to know why.

Also, the output, as you can see from the first link is 29. I pseudo-understand what’s happening: sizeof(int) is 4, and when I wrote a+3 instead of adding 3 to 17, 3*4 is being added.

Still, I would appreciate if someone can explain it more eloquently.

Thanks!

  • 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-27T04:36:32+00:00Added an answer on May 27, 2026 at 4:36 am

    For C++ version try this:

    int *a = (int *)17;
    printf("%d", a + 3);
    

    And yes your explanation is correct…

    This is called pointer arithmetic and works the way you said. Note however that the code you are using here where you assign a constant value to a pointer to an int is rarely done in real life. The pointer value usually comes from some kind of memory allocation functions e.g malloc in C or new in C++ (although new isn’t a function).

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

Sidebar

Related Questions

I've been tinkering with some code in a effort to understand OOP using c.
In an effort to 'clean up' my code - I was wondering if this
I believe this code may require more effort to comprehend than average, so I'm
This is my 2nd post on this site in my effort to understand the
As an exercise and in an effort to (better understand|help other people better understand)
I am making the effort to learn Vim. When I paste code into my
This is my first effort on solving the exercise. I gotta say, I'm kind
In an effort to understand MVC 2 and attempt to get my company to
As I understand it, the Freebase taxonomy generally boils down to this hierarchy: Domain
I understand that sessions in HTTP is stateless and hence we have methods like

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.