Thread: Can pointer directly store data

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    90

    Can pointer directly store data

    I do not understand how the codes below work


    Code:
    #include<stdio.h>
    
    int main ()
    {
    	char * p = { "Hello"};
    	
    	printf("%c", *p);
    	
    	return 0;
    }
    code output : H

    Pointer hold memory location of another variable. The program has a variable named pointer

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    "Hello" is a string constant, so it exists independently of the pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    90
    Quote Originally Posted by laserlight View Post
    "Hello" is a string constant, so it exists independently of the pointer.
    If the string is constant, so const is not written in the code anywhere. How do you know the string is constant

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Player777
    If the string is constant, so const is not written in the code anywhere. How do you know the string is constant
    "Hello" is a string constant because it is a string constant by definition. Likewise, in this program below, 123 is an integer constant because it is an integer constant by definition, no const keyword is necessary:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("%d\n", 123);
        return 0;
    }
    Last edited by laserlight; 02-15-2020 at 04:06 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Quote Originally Posted by Player777 View Post
    If the string is constant, so const is not written in the code anywhere. How do you know the string is constant
    You don't. The string literal could be constant or non-constant (it's not defined by the C Standard). But treating it as non-constant is undefined behaviour so best to just always treat it as constant

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read data off USB stick directly somehow
    By BpB in forum Tech Board
    Replies: 17
    Last Post: 07-16-2017, 12:30 AM
  2. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  3. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  4. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM
  5. Best way to store this data and connections to other data
    By blackwyvern in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 02-01-2002, 05:06 AM

Tags for this Thread