PDA

View Full Version : String pointer questions in C.




francis21
Feb 6, 2012, 05:13 PM
found a solution. Thanks guys! :)



robbieduncan
Feb 6, 2012, 05:21 PM
s is a char * variable. So *s should point to a char. "" is not a char. It's a zero length string. A string is an array of characters. So this is effectively a pointer. Which is, effectively, an int. Hence on this line

*s = "";

you get a warning as the type on the right is a pointer and the expected type is a character.

KnightWRX
Feb 6, 2012, 05:26 PM
Use the code tags please.

Also, you should really think about properly indenting your code for greatly improved readability. Lastly, your error is a punctuation error. In C, what does "" represent ? What type is *s in your normalize function ?

Right there should tell you what you're doing wrong.

Also, you should rethink your function to remove the return statements, they are completely unneeded.

Mac_Max
Feb 6, 2012, 05:28 PM
One problem is in this line:

if (('0' <= *s && *s <= '9') || *s == ' ') {

The compiler is yelling at you because your char pointer (and your '0' and '9' chars) is being de-referenced and cast into an integer to use the <= operator. There should be some string/char comparison functions in the c standard library that you can use instead. *hint hint* ;).

francis21
Feb 6, 2012, 05:29 PM
s is a char * variable. So *s should point to a char. "" is not a char. It's a zero length string. A string is an array of characters. So this is effectively a pointer. Which is, effectively, an int. Hence on this line

*s = "";

you get a warning as the type on the right is a pointer and the expected type is a character.

So in my code above, it make sense that "" represents a char *. However, let say if I see a digit or a space, how would I represent an empty char? The following '' would not work since the compiler complains that it is an empty char.

My goal on that part of my code is to kind of "concatenate" (not the best word to explain my goal) the strings together. So that is produces the expected output. By putting an empty string where *s points at that moment allows the other strings to be pushed over.

robbieduncan
Feb 6, 2012, 05:31 PM
One problem is in this line:

if (('0' <= *s && *s <= '9') || *s == ' ') {

The compiler is yelling at you because your char pointer (and your '0' and '9' chars) is being de-referenced and cast into an integer to use the <= operator. There should be some string/char comparison functions in the c standard library that you can use instead. *hint hint* ;).

I don't think so. Unless things have changed a lot since I learnt C many years ago you can use <= on chars.

----------

So in my code above, it make sense that "" represents a char *. However, let say if I see a digit or a space, how would I represent an empty char? The following '' would not work since the compiler complains that it is an empty char.

My goal on that part of my code is to kind of "concatenate" (not the best word to explain my goal) the strings together. So that is produces the expected output. By putting an empty string where *s points at that moment allows the other strings to be pushed over.

There is no such thing as an empty char. You cannot insert such a char (as it does not exist) into the position in the string to concatenate. You need to move all the other chars one position down including the \0 terminator char.

Mac_Max
Feb 6, 2012, 05:32 PM
I don't think so. Unless things have changed a lot since I learnt C many years ago you can use <= on chars.

Could be a C++ thing then. I tend to backport knowledge from C++ to C and this could be one of those differences that don't match up or just a compiler specific thing. I seem to remember getting yelled at by the compiler in Microsoft VC++ (possibly because of localization/portability concerns between source files).

KnightWRX
Feb 6, 2012, 05:33 PM
One problem is in this line:

if (('0' <= *s && *s <= '9') || *s == ' ') {

The compiler is yelling at you because your char pointer (and your '0' and '9' chars) is being de-referenced and cast into an integer to use the <= operator. There should be some string/char comparison functions in the c standard library that you can use instead. *hint hint* ;).

That's completely untrue. If you'd bothered to copy/paste and compile his code, you'd know that is not the line the compiler complains about.

gnasher729
Feb 6, 2012, 05:37 PM
So in my code above, it make sense that "" represents a char *. However, let say if I see a digit or a space, how would I represent an empty char? The following '' would not work since the compiler complains that it is an empty char.

My goal on that part of my code is to kind of "concatenate" (not the best word to explain my goal) the strings together. So that is produces the expected output. By putting an empty string where *s points at that moment allows the other strings to be pushed over.

Take a deck of cards. Lay out the cards in a row, side by side.

If you want to paint all red cards in black, you can do that without moving any cards.

But if you want to pain all red cards in black, _and_ you want to remove all aces, but without leaving gaps, you'll have to move cards around.

Same thing here.

francis21
Feb 6, 2012, 05:41 PM
First of all, thanks for the person above who edited my code, since I just copy-and-paste my code from CodeRunner. :)

Use the code tags please.

Also, you should really think about properly indenting your code for greatly improved readability. Lastly, your error is a punctuation error. In C, what does "" represent ? What type is *s in your normalize function ?

Right there should tell you what you're doing wrong.

Also, you should rethink your function to remove the return statements, they are completely unneeded.

Punctuation error regarding ""? It is kind of vague on what you mean by that. "" is an empty string (or char *). Also, I know that while going through the string s, *s currently looks at the value that s is currently pointing to.

robbieduncan
Feb 6, 2012, 05:44 PM
First of all, thanks for the person above who edited my code, since I just copy-and-paste my code from CodeRunner. :)

You're welcome (only Moderators and Gods can edit other people's posts). If you post more code the [ code ]code here[ /code ] (without the spaces in the tags) tags should be used. I didn't need to edit/alter it apart from the tags: copy and paste seems fine.

Oh and I don't think "punctuation error" is really the correct terminology. You have a logic error in thinking you can set one of the characters in the string to be "blank" or "empty".

Mac_Max
Feb 6, 2012, 05:51 PM
That's completely untrue. If you'd bothered to copy/paste and compile his code, you'd know that is not the line the compiler complains about.

Ouchies, I love you too haha. Sadly I don't have one on me 24/7 but that's not to say one can't read source code and detect an error. If I was wrong this time, oh wells. I'll let them cut my hands off/hang/draw and quarter me later.

That said, looking at it again, I realize what compiler error I thought I had seen (and so you don't have to pull out a compiler, I already know isn't the same warning/error):


void normalize(std::string *s){
...
if (('0' <= *s && *s <= '9') || *s == ' ') {
...
}


I guess that's what happens when you spend too much time in C++ land.

francis21
Feb 6, 2012, 05:51 PM
You're welcome (only Moderators and Gods can edit other people's posts). If you post more code the [ code ]code here[ /code ] (without the spaces in the tags) tags should be used. I didn't need to edit/alter it apart from the tags: copy and paste seems fine.

Oh and I don't think "punctuation error" is really the correct terminology. You have a logic error in thinking you can set one of the characters in the string to be "blank" or "empty".

That makes more sense. I've given it some thought and I think you just can't really place an "empty" string at that given spot where s is currently pointing at (the value I mean). So I'm guessing this requires looking "ahead" of the next characters and placing the next character found to the spot where I have encountered a digit or a space already.

KnightWRX
Feb 6, 2012, 07:18 PM
Oh and I don't think "punctuation error" is really the correct terminology.

I'm referring to "" vs ''. Quotes rather than punctuation if you don't like the word. Using '\0' would have removed his warning (which is what his original post was referring to, the warning was about converting the string pointer and placing it into a char without a cast). Of course, that wouldn't have fixed his function to do what he intended to do though.

----------

That makes more sense. I've given it some thought and I think you just can't really place an "empty" string at that given spot where s is currently pointing at (the value I mean). So I'm guessing this requires looking "ahead" of the next characters and placing the next character found to the spot where I have encountered a digit or a space already.

Actually, I wouldn't over complicate matters with looking ahead and stuff. Just implement a simple function :


char * removecharacteratindex(char * string, int index);


use the return value to indicate an error (null pointer) or success (the new string). Basically, you should check that index is not greater or equal than strlen() - 1, and then using some pointer trickery, just create 2 substrings by substituting index with '\0', then strncat()'ing them back together.