# Thread: Pointer of a char digit - C

1. ## 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?

2. Are you trying to extract the digit chars in the input string to form an output string consisting of those digits?

3. Originally Posted by laserlight
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. 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?

5. 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).

6. Originally Posted by john.c
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.

Originally Posted by laserlight
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. 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!

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:
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.

8. 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.

9. Originally Posted by laserlight
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!!
Originally Posted by Zeus_
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. > 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
}```

11. Originally Posted by Zeus_
> 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. Please stop "quoting" entire posts for no reason! It's just more crap to scroll through.

13. Originally Posted by john.c
Please stop "quoting" entire posts for no reason! It's just more crap to scroll through.
No problem.

Popular pages Recent additions