Thread: char*

  1. #1
    zach
    Guest

    char*

    Can someone please explain what kind of whatever this (char*) is?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    691
    Quote Originally Posted by zach View Post
    Can someone please explain what kind of whatever this (char*) is?
    It is a pointer.

  3. #3
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    It's a pointer to an array of characters.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,543
    It's a pointer to an array of characters.
    No, it's a pointer. It could be either a pointer to an array of char or a pointer to a single char. Without more content you can't tell which.

  5. #5
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    zach, 15 hours ago you made a thread called "puzzled by char*" and then you said my response cleared up a lot for you.

    8 hours ago, you made this thread with 1 sentence asking what a char* is again. It may be a good idea to condense these down to 1 thread so that you can just get all of your very related char* questions answered.

    As I wrote in the other thread, char* is a POINTER (which is a variable that holds a memory address). This pointer holds the memory address of a char data type. As mentioned above, it can be a single char, or it can be a char array or string because those just have multiple contiguous chars in a row.

    When you see (char*) exactly like that in code, this means that the programmer is casting something to a char*. Look up C casting for more info on a search engine.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,833
    More accurately, a char* is a pointer to a char, with a few caveats. This char may or may not be part of an array of char. The caveats would be that instead of actually pointing to memory that can be validly interpreted as a char, the pointer might point one past the end of an array of char, or it could be a null pointer, or it could be invalid. Nonetheless, in terms of type it remains a pointer to char. Loosely speaking, we commonly say that such a pointer points to an array of char or to a string when it points to the first char in an array or string, but strictly speaking a pointer to an array of char is a different type (i.e., char (*)[N] for some array of N chars).
    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

  7. #7
    zach
    Guest
    Laserlight, is this what you are saying?

    "In general a pointer points to an array when it points to the first char of that array.

    Specifically, a char* is a pointer to a char That char might be a singular char or might be an array of char.

    The pointer could be a null pointer, be invalid, or point one past the end of an array of char."

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    691
    Quote Originally Posted by zach View Post
    Laserlight, is this what you are saying?

    "In general a pointer points to an array when it points to the first char of that array.
    Specifically, a char* is a pointer to a char That char might be a singular char or might be an array of char.
    The pointer could be a null pointer, be invalid, or point one past the end of an array of char."
    These 3 sentences are crystal clear to me... You could think about pointers as objects which holds a memory address. Since a process has its own "address space", an address can be valid (inside the process "address space"), invalid (outside the process "address space") or NULL (zero, usually - which is an invalid address as well).

    An address "points" to something in memory. This "something" have size and that's what you specify in the pointer declaration:
    Code:
    char *p;
    Here, the variable p is a pointer (holds an address) which points somewhere in memory (when initializaed). But, when used to access memory, it will read/write chars. When you do some arithmetic with p variable (+ or -) it will take in consideration the size of data pointed, like:
    Code:
    char a[] = "fred";
    int b[] = { 1, 2, 0 };
    char *p;
    int *q;
    
    p = &a[0];  // p now have the address of the first element of array a
    q = &b[0];  // q now have the address of the first element of array b.
    p++;  // increment p (the address which p holds) in 1 unit (sizeof(char)).
    q++;  // increment q (the address which q holds) in 4 units (sizeof(int)).
    So q now points to the second element of the array b!

    PS: Multiply and divide the pointer by an value isn't wise (and the compiler will issue an warning)...

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    691
    Maybe it is better do draw...
    In the fragment:
    Code:
    char a[] = "fred";
    int b[] = { 1, 2, 3, 0 };
    char *p = &a[0];
    int *q = &b[0];
    p and q ponits (holds the address) of the first elements of the arrays:
    char*-a1-png
    char*-b1-png

    After incrementing p and q:
    Code:
    p++;
    q++
    This will happen:

    char*-a2-png
    char*-b2-png

    It is better to see the integers, in little endian format and what happens to q after it is incremented:

    char*-b1a-png
    Last edited by flp1969; 08-21-2019 at 04:38 PM.

  10. #10
    zach
    Guest
    flp1969,

    *All* I was asking was whether the quote below was a correct understanding of what silverlight had said. I **wasn't** asking what an ordinary pointer is. In my perception a char* isn't just an ordinary pointer to something. Nevertheless, thank you for responding.

    "In general a pointer points to an array when it points to the first char of that array.
    Specifically, a char* is a pointer to a char. That char might be a singular char or might be an array of char.
    The pointer could be a null pointer, be invalid, or point one past the end of an array of char."

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    691
    Quote Originally Posted by zach View Post
    *All* I was asking was whether the quote below was a correct understanding of what silverlight had said. I **wasn't** asking what an ordinary pointer is. In my perception a char* isn't just an ordinary pointer to something. Nevertheless, thank you for responding.
    You're welcome.... Who's "silverlight"?

  12. #12
    zach
    Guest
    Re You're welcome.... Who's "silverlight"?

    Silverlight is a contributor to this forum who really knows his stuff.

  13. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    691
    Quote Originally Posted by zach View Post
    Re You're welcome.... Who's "silverlight"?

    Silverlight is a contributor to this forum who really knows his stuff.
    Don't you mean laserlight? And her stuff?

  14. #14
    zach
    Guest
    Re: Don't you mean laserlight? And her stuff? Yes of course.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    949
    Quote Originally Posted by zach View Post
    her
    Best to make no assumptions.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread