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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:31:31+00:00 2026-06-12T19:31:31+00:00

I would like to understand how pointers work, so i created this small program.

  • 0

I would like to understand how pointers work, so i created this small program. first of all i create a p pointer, which points to a char.

The first question is at this point. If i create a pointer, the value of it is a memoryaddress (if i point it to a non-pointer object), but this time it is “haha” in my example. Why does it work this way in char*? And how i can add value to it with cin >> p?

My second question is that, i created a q char, which has the value of the *p pointer at the point i created it. BUT its value and address are “h” too, but why? It must be the memory address of this char object! It’s pointless 😀 (mingw – gcc)

#include <iostream>

int main() 
{
 /* char *p;
    cin >> p;                      //forexample: haha */

    char * p = "haha";
    char q = *p;
    std::cout << "&q = " << &q << std::endl;   //&q = h
    std::cout << "q  = " <<  q << std::endl;   //q = h

    return 0;
}

MORE: If i allocate memory first with char a[100]; char *p=a; then &q = h»ŢĹ, so “h” and some mess. but it should be a memoryaddress! and my question is, why is not it address then?

  • 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-12T19:31:32+00:00Added an answer on June 12, 2026 at 7:31 pm

    Think of char* p; as of address in memory. You did not initialize this pointer so it does not point to anything, you cannot use it.

    To be safe always:
    either initialize pointer to zero:

    char *p = 0; // nullptr in C++11
    

    or initialize to some automatic

    void foo() {
      char a[100];
      char *p = a;
    }
    

    or global memory:

    char a[100];
    void foo() {
      char *p = a;
    }
    

    or get dynamic memory:

    char* p = new char [100];
    

    Then you can use p (if not NULL) to read data and to read from p…


    For your misunderstaning of operator >> (std::istream&, char* p). This operator expects that p points to some memory (automatic,global,dynamic – no matter) – it does not allocate memory by itself. It just reads characters from input stream until whitespace and copy it to the memory pointed by p – but p must already points to some memory.


    For taking address of char q;. Of course you can take address of q: &q, and it type is char* p. But &q is different that p, and this q=*p just copies first character pointed by p to q, it cannot change address of q – its address is unchangeable. For cout << &q – operator << (ostream&, char* p) expects that p points to NULL terminated string – and &q points to memory containing "H" but what is after this character no one knows – so you will get some garbage on screen. Use cout << q to print single character.

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

Sidebar

Related Questions

I would like to understand how Etherpad's timeline feature work. If you don't know
I need to understand how this is possible, i.e. I would like to understand
I would like to understand what Implement Interface Explicitly entails in C# based on
I would like to understand whats the best way to handle exceptions in Multicast
I would like to understand that what is Mesh Object and its conection with
I would like to understand what class << self stands for in the next
I would like to understand how object deletion works on python. Here is a
I am new to silver light and would like to understand a bit more
I am developing a Rails application and would like to understand when to use
I'm new to the subject of encoding and would like to understand it in

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.