Stacks with chars as pointers...a bit confused.

So I'm supposed to create a stack that checks to see if brackets are balanced; IE, {()} is balanced where as {(}) isn't. I've figured out the algorithm to check to see if they're balanced.

However, the assignment is to take in a char pointer/string (char *something) as a parameter, and I'm just confused how to traverse through each letter.

I feel like this is incredibly basic too, but my brain is melting due to finals coming up so I'm not thinking properly.
just use
1
2
char* something = "_ello";
something[0] = "H"

pointers can be accessed like arrays
(and remember a string ends with '\0')
Last edited on
Thanks!

As for traversing through each element in the array, I know that I'd have to use a for loop--but which of these would be efficient/safer?

1
2
for (size_t i = 0; i != '\0'; i++)
{...}


...Or...

1
2
for (size_t i = 0; i < strlen(i); i++)
{...}
Last edited on
dont use:
1
2
for (size_t i = 0; i != '\0'; i++)
{...}


use
1
2
for (size_t i = 0; chars[i] != '\0'; i++)
{...}

:P

both are equally safe, because strlen just goes through the string until '\0' is found
Oops, that's what I meant. Thanks!
Topic archived. No new replies allowed.