0

I am a bit confused by all the new things about pointers. We can access a the value of a pointer by dereferencing it. For example, if you have char* str, you access the information that str is pointing to by using *str or str-> ? However how does this work if you want to access the values in order to put inside a function? like atoi(*str)?

Also, if you want to access the first character a char* pointer is pointing to, can you do *pointer[0]?

My relevant codes in the check function are as following

char check[strlen(word)+1];  
strcpy(check,word);

int character = atoi(check[0]);

compiler errors

0

Q1. How does this work if you want to access the values in order to put inside a function? like atoi(*str)

Realize that char* isn't new in the course, you've been using them all along, just calling them "string" instead. Every time you wrote string before the CS50 library followed behind you and literally erased "string" and changed your text to "char*". You can go back to any of your old programs and change all string text to char* and it will work the same.

So did you dereference the value before? Nope. And you don't do it now either, atoi(str). It is useful to remind yourself how a char* works.

char* str = "hello";

The address that is stored in str is the location of 'h', if you dereference str, all you will get is 'h'. Which isn't of much use to atoi. But by declaring that this a char* we are stating that all the values to the right are part of our string until you hit '\0'. All that is to say, typically you will pass the char* into a function.

Q2. if you want to access the first character a char* pointer is pointing to, can you do *pointer[0]?

So there are two ways to do this, pointer notation and array notation (you've done both) and you need to chose one. *str will dereference the first value of the string, str[0] will also dereference the first value of the string.

Q3. Why are you getting an error?

Well the function atoi is expecting a char* (or a char[]), but check[0] is a char. The below code is made syntactically correct by deleting the [0] after check, but it doesn't have the same functionality (I'll leave that for you to figure out).

char* word = "12 eggs in a dozen";
char check[strlen(word)+1];  
strcpy(check,word);
int character = atoi(check);
printf("number: %i\n", character);  
1
  • Thank you very much! – Florence Wu May 2 '15 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .