Thread: Pointer of a char digit - C

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

    Pointer of a char digit - C

    Hey everyone.

    Suppose we have char pointer passed by a function.
    In the function we're iterating the chars within this char pointer array.

    If we catch that some char is digit we want to assign it to another char pointer, this way:

    Code:
    if (isDigit(text[i]) {
         char* digit = &(text[i] + '\0');
    }
    What's the problem with this approach?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you trying to extract the digit chars in the input string to form an output string consisting of those digits?
    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
    135
    Quote Originally Posted by laserlight View Post
    Are you trying to extract the digit chars in the input string to form an output string consisting of those digits?
    Yes.

    But, generally, I want to understand if it's legal to define pointers to some elements of arrays.
    Is it okay?..

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    It's perfectly fine to do that, but the + '\0' part doesn't make any sense. Actually, the code overall makes no sense since it does nothing, so it's not a good representation of your problem. What exactly are you trying to do?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HelpMeC
    But, generally, I want to understand if it's legal to define pointers to some elements of arrays.
    Is it okay?..
    Yes, that's perfectly fine. For example:
    Code:
    char* digit = &text[i];
    The problem is that you're not doing that. Rather, you're creating a temporary char with (text[i] + '\0'), and then trying to take the address of that temporary, but that's not allowed.

    Since text[i] is already a digit char, there is no need to add '\0' to it: that technique is used when you have an int digit value and want to convert it to a digit char. EDIT: wait a minute, you're not adding '0', you're adding '\0', which is equivalent to adding 0, i.e., it has no net effect except to result in a temporary. I'm guessing you thought you could construct a string in this way.

    But, even if you do use my example, that still doesn't work for your overall goal: you would only have a pointer to a particular char in the input string. Rather, you should allocate space for the output string, then copy the relevant chars over to construct the result (remembering to null terminate the string).
    Last edited by laserlight; 12-01-2019 at 04:42 PM.
    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

  6. #6
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by john.c View Post
    It's perfectly fine to do that, but the + '\0' part doesn't make any sense. Actually, the code overall makes no sense since it does nothing, so it's not a good representation of your problem. What exactly are you trying to do?
    I want to examine one's ability to define pointers to particular elements of another array.

    Quote Originally Posted by laserlight View Post
    Yes, that's perfectly fine. For example:
    Code:
    char* digit = &text[i];
    The problem is that you're not doing that. Rather, you're creating a temporary char with (text[i] + '\0'), and then trying to take the address of that temporary, but that's not allowed.

    Since text[i] is already a digit char, there is no need to add '\0' to it: that technique is used when you have an int digit value and want to convert it to a digit char. EDIT: wait a minute, you're not adding '0', you're adding '\0', which is equivalent to adding 0, i.e., it has no net effect except to result in a temporary. I'm guessing you thought you could construct a string in this way.

    But, even if you do use my example, that still doesn't work for your overall goal: you would only have a pointer to a particular char in the input string. Rather, you should allocate space for the output string, then copy the relevant chars over to construct the result (remembering to null terminate the string).
    So I understand from your example and explanations that it's fine to define pointers to some elements of another array.

    As for your code line: why is it correct?
    You have a char pointer named digit, and you are assigning to it a char's address - does it make sense?

    Or you might say that text[i] in this case is represented as integer so we take the address of an integer and assign it to char pointer - so don't we have to do casting to char* at least?

    I am a little confused regarding this issue of pointers to a single char really...

    Thank you guys!!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by HelpMeC
    As for your code line: why is it correct?
    You have a char pointer named digit, and you are assigning to it a char's address - does it make sense?
    The value of a pointer is an address of some type. So yes, assigning the address of a char to a pointer to char makes sense: it makes so much sense that it is exactly the expected thing to do! It's like looking at assigning 123 to an int variable and asking if that makes sense (context aside). Of course it does!

    Quote Originally Posted by HelpMeC
    Or you might say that text[i] in this case is represented as integer so we take the address of an integer and assign it to char pointer - so don't we have to do casting to char* at least?
    You wrote:
    Quote Originally Posted by HelpMeC
    Suppose we have char pointer passed by a function.
    In the function we're iterating the chars within this char pointer array.
    So text[i] is a char, isn't it? Why do you suddenly want to promote it to an int when the situation (taking its address) doesn't call for an integer promotion?

    You take the address of a char, you get a pointer to char. It is absolutely pointless to cast a known pointer to char to be a pointer to char.
    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

  8. #8
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    text[i] is a character as she explains, not an integer.

    Write a simple program to understand characters.

    Code:
    char c = '1';
    
    cout << c << " " << (int) c;
    You get the output as 1 49. When you have a number stored in char, it is not actually the decimal number you think it stores but the equivalent ASCII value. So, if you wrote char c = 49 and then output it, you'd get the number 1 displayed.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  9. #9
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by laserlight View Post
    The value of a pointer is an address of some type. So yes, assigning the address of a char to a pointer to char makes sense: it makes so much sense that it is exactly the expected thing to do! It's like looking at assigning 123 to an int variable and asking if that makes sense (context aside). Of course it does!


    You wrote:

    So text[i] is a char, isn't it? Why do you suddenly want to promote it to an int when the situation (taking its address) doesn't call for an integer promotion?

    You take the address of a char, you get a pointer to char. It is absolutely pointless to cast a known pointer to char to be a pointer to char.
    Fair enough - thank you!!
    Quote Originally Posted by Zeus_ View Post
    text[i] is a character as she explains, not an integer.

    Write a simple program to understand characters.

    Code:
    char c = '1';
    
    cout << c << " " << (int) c;
    You get the output as 1 49. When you have a number stored in char, it is not actually the decimal number you think it stores but the equivalent ASCII value. So, if you wrote char c = 49 and then output it, you'd get the number 1 displayed.
    So, related question to the original one - in case I have found that some char has an integer value (isDigit returns true) - and I want an integer pointer to point to the address of this digit.
    Do I have a legal possible way at all?

    I should take this char and add/substract from it '0' - giving me the integer value of it.
    Now, suppose I want to take its address -
    Code:
    int* digit = &(text[i] + '0');
    What's wrong with this scenario? That I'm trying to point to some integer value which does not really exist in the memory as integer?

    Thank you!

  10. #10
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > What's wrong with this scenario? That I'm trying to point to some integer value which does not really exist in the memory as integer?

    Yes, text[i] + '0' produces a temporary and you cannot take its address as assign to a pointer.

    >
    I should take this char and add/substract from it '0' - giving me the integer value of it.

    If the char is between '0' and '9', it is a digit and that's what isDigit () should be checking for. It's the same as checking if that char is b/w integer 48 (ASCII value of 0) and 57 (ASCII value of 9). So, you should not be adding '0' to the number to obtain it's integer value but subtracting '0' (i.e. subtracting 48).

    char '2' - int 50
    subtract '0' i.e. subtract 48 from int 50, you get int 2. And that's your integer interpreted value of an isDigit () character.

    Code:
    bool isDigit (char c)
    {
          if (c >= '0' && c<= '9') return true; // can also be written as if (char >= 48 && char <= 57) return true;
    
          return false;
    }
    I'm curious to know where and why would you wanna be doing this? Explain to us what you're intending to do and why you need an integer pointer to the integer value of text[i]. You could just have a variable, let's say value, and take it's address this way:

    Code:
    int value;
    
    if (isDigit (text[i]))
    {
          value = text[i] - '0'; // or text[i] - 48
    
          // Use value address by reference & operator 
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  11. #11
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by Zeus_ View Post
    > What's wrong with this scenario? That I'm trying to point to some integer value which does not really exist in the memory as integer?

    Yes, text[i] + '0' produces a temporary and you cannot take its address as assign to a pointer.

    >
    I should take this char and add/substract from it '0' - giving me the integer value of it.

    If the char is between '0' and '9', it is a digit and that's what isDigit () should be checking for. It's the same as checking if that char is b/w integer 48 (ASCII value of 0) and 57 (ASCII value of 9). So, you should not be adding '0' to the number to obtain it's integer value but subtracting '0' (i.e. subtracting 48).

    char '2' - int 50
    subtract '0' i.e. subtract 48 from int 50, you get int 2. And that's your integer interpreted value of an isDigit () character.

    Code:
    bool isDigit (char c)
    {
          if (c >= '0' && c<= '9') return true; // can also be written as if (char >= 48 && char <= 57) return true;
    
          return false;
    }
    I'm curious to know where and why would you wanna be doing this? Explain to us what you're intending to do and why you need an integer pointer to the integer value of text[i]. You could just have a variable, let's say value, and take it's address this way:

    Code:
    int value;
    
    if (isDigit (text[i]))
    {
          value = text[i] - '0'; // or text[i] - 48
    
          // Use value address by reference & operator 
    }
    Very informative and helpful reply - thank you.
    Understood.

    And, I wanna explore as much as possible the flexibility of C language and its restrictions - both to expand my knowledge and abilities, and to get ready to the exam I have on Friday in C.

    Thank you!!!

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Please stop "quoting" entire posts for no reason! It's just more crap to scroll through.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  13. #13
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by john.c View Post
    Please stop "quoting" entire posts for no reason! It's just more crap to scroll through.
    No problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 10-25-2017, 06:12 AM
  2. Help: Storing tokenized 2-digit char (infix->postfix converter)
    By misterpogos in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2011, 11:19 AM
  3. Read from file - 1-digit and 2-digit numbers
    By Bonaventura in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 06:33 AM
  4. digit, char, etc..
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-23-2002, 01:35 PM
  5. Validate digit or char!?
    By netboy in forum C Programming
    Replies: 8
    Last Post: 12-23-2001, 07:11 PM

Tags for this Thread