Jump to content
  • Advertisement
Sign in to follow this  
mcarrier

searching char pointers

This topic is 5398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I have a quick question for the professionals here: I have a program which is taking in main(int argc, char **argv). When I pass these in, some have spaces (because they are pathways to files). So, when I create these to pass in to the C program I put '*' where the ' ' is. I now need to change the '*' back to ' ', but I can not figure out how to do it with a pointer and not an array. And C will not let me change the pointer to an array. Any Suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Here's a short example how to access an array element with a pointer pointer:


#include <iostream>

int main()
{
//create a pointer pointer in 2 steps
char c[] = "hey";
char* pc = c;
char** ppc = &pc;

//modify c using the pointer pointer
(*ppc)[1] = 'E';

std::cout <<c <<std::endl;

return 0;
}



\Jimmy H

Share this post


Link to post
Share on other sites
I read my post again and realized maybe I should explain it more. c is contains the address to the first element in the array c[] so pc will point at that element. &pc returns the address to pc so ppc will point at pc. This makes ppc similar to char** argv.

It's possible to access c by using ppc. First retrieve the variable ppc is pointing at using (*ppc). That is pc which is equal to c. So (*ppc)[1] is the same thing as writing c[1] which is 'e'. c[1] is then set to 'E' and displayed to demonstrate it worked.

\Jimmy H

Share this post


Link to post
Share on other sites
Quote:
Original post by mcarrier
...I can not figure out how to do it with a pointer and not an array.
An array is a pointer.

More completely, an array is merely a sequence of adjacent memory locations referenced by the address of the first element. But addresses in memory are pointers, which means that an array is a sequence of adjacent memory locations referenced by the pointer to the first element.

C doesn't perform bounds checking, which means that you can write beyond the ends of an array. This will often result in an access violation, but only if the memory you attempt to address does not belong to your process (and sometimes not even then: if you're running with supervisor priveleges, for instance). So your problem is finding out how long each sequence of characters pointed to by a char * is.

You're in luck. C interprets char * sequences specially under certain circumstances. This isn't a language difference; it's merely a set of conventions to consider a char * sequence as a single string. The string is terminated by a null character, which has the integer value of 0 and the ASCII character '\0'. The C standard library provides a plethora of functions for dealing with strings constructed this way, though they may create certain security vulnerabilities within your code.

Anyway, to obtain the length of a null-terminated character array, use the strlen function, which is declared in the <string.h> header (<cstring> if you're using a C++ compiler). This returns a size_t value which is the number of characters in the array. Another function that may interest you is strchr, which searches for the first instance of a particular character within the array.

All that said, the real solution is to quote the values you pass on the command line:
C:\>myprog 1 2 "This entire string is value 3"

Enjoy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!