0

I'm given a method header as so:

char* thisMethod(char* input){}

is it possible to say,

char var = input[0];

? I know that "input" will be in the form of a char array.

I'm obviously new to C++ pointers are throwing me off. I tried researching how to work with char pointer function arguments but couldn't find anything specific enough to help. Thanks for the help.

2
  • 1
    Is that second line of code intended to go inside the function? If so, write it that way. And, yes, if you do that, it will copy the character that input points to into var. Commented Sep 14, 2016 at 14:21
  • Picking at nits, all you can guarantee is that there is one character that the pointer is pointing to. Anything else is an assumption. For passing more than one character, use std::string or std::vector<char> or some other container. Commented Sep 14, 2016 at 16:28

3 Answers 3

1

There is a misconception that may lead you into further troubles:

I know that "input" will be in the form of a char array.

NOPE: By the scope of that function, input is a pointer to a character. The function has no way to know where such a pointer comes from.

If it has been taken from an array upon calling the function than it will be a pointer to the first element of that array.

Because pointer have an arithmetic that allows to add offsets and because the [] operator applied to pointers translates as a[b] = *(a+b) by definition, in whatever code, if a is a pointer, *a and a[0] are perfect synonymous.

Think to an array as a sequence of boxes and a pointer as your hand's index finger

adding an offset to a finger (like finger+2) means "re-point it aside" and de-referencing it (like *finger) means "look inside what it points to".

The [] operator on pointers is just a shortcut to do both operations at once.

Arrays are another distinct thing. Don't think to them when dealing with pointers, since -in more complex situations, like multidimensional array or multi-indirection pointers - the expression a[b][c] won't work anymore the same way.

Sign up to request clarification or add additional context in comments.

Comments

0

There are two ways to get a value from a pointer, * and []. The following are equivalent:

char var1 = *input;
char var2 = input[0];

Using the brackets is more common when you know you were passed an array, since it allows you to supply an index. You need some way of knowing where the end of the array is so that you don't attempt any access past it, your function is missing that important detail.

2 Comments

Now would it be ok to use input[1] input[2] etc to continue down the char array that I know is being looked at by the pointer? Or is there a way to place the char array into a string so it is easier to track the size/boundaries of the char[]/string?
@BrandonWalker The usual convention with strings is for every character to be valid until you reach a character with a value of zero. Not '0', but 0. If you have that you can easily copy it to a std::string with simple assignment.
0

As long as it's inside the function and input points to something valid ( not NULL/nullptr and not a garbage location ) then doing char var = input[0]; is just fine. It's the same as char var = *input.

P.S If it's supposed to be a string I recommend using std::string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.