1

Can someone help me understand why I would be getting "warning: cast to pointer from integer of different size" for the following two lines of code?

so I have a pointer to a string (char *string) and a double pointer (char **final) that needs to the store the address of the last char in string... I thought the following lines of code would work but I keep getting the error... How do I fix it?

char last = *string; 
*final = (char *)last;
4
  • What do you think those lines do individually? Neither one makes sense for doing what you want, but I'm not sure how to explain why not without knowing what you think they do. Commented Feb 8, 2018 at 0:14
  • last is char (integer), *final is a pointer to char. You are casting an integer into a pointer. The warning makes it very clear. Commented Feb 8, 2018 at 0:18
  • So I'm at the last character of string (aka string = 'c') and I'm trying to make my char* final pointer store the address of the character 'c' that's in string. Does that logic make sense? Commented Feb 8, 2018 at 0:18
  • *final = string; Commented Feb 8, 2018 at 0:20

3 Answers 3

1

If string is a pointer to the last character in the string, last is a copy of that character. Since it's just a copy of the value, it bears no relationship to the location in the original string. To save that pointer into what final points to, you should do:

*final = string;
Sign up to request clarification or add additional context in comments.

Comments

1
(char *)last

last is of type char. Casting it to a pointer means the numeric code of the character stored in last will be interpreted as an address. So if last contains A, then this will cause the value 65 to be interpreted as an address. (Assuming ASCII). The compiler is smart and indicates that this is probably not the behavior you intend.

1 Comment

Oh I see... so how would I point to the address that last is at?
0

To declare a variable you have to specify what type you want the variable to be, and then what you want to call the variable. If you want a variable of type "char", called "last", it can be achieved by the following syntax:

char last;

If you want a pointer to a variable of a certain data type, you add the asterisk symbol like so:

char *last;

Now you have a pointer that you can use to point at a place in memory which have to contain a char. If you are trying to create a "string" in c, that is nothing more but a series of char's, that are ordered consecutively in memory. You can use a char pointer to point at the first char in this series of char's, and then you can use specific functions that work on strings (for example strcpy or strlen), by giving this char pointer as input argument.

Now to your problem. Let's say you create a string like this:

char *str = "example";

what you have done is create a series of char's, namely

'e', 'x', 'a', 'm', 'p', 'l', 'e', '\0'

(where the '\0' is the NULL character that marks the end of the string. This is necessary for any functions working on strings to recognize where the string ends). The char pointer you have created called "str" points at the first char, that is 'e'. Remember, the pointer has the address of this char, and all the rest of the chars are stored in the address space following this first char.

To access a particular char in this string, you have to dereference the pointer "str". If you want the first char in the string, you do this:

char first = *char;

This will save the first char in a variable of type char called "first", that is in this case the letter 'e'. To get the second char you do this:

char second = *(char+1);

What you're actually doing is "reading" (dereferencing) the value that your char pointer "str" is pointing to + 1 step of size "char" in memory. In this example, this means that the variable of type char called "second" now contains (the ASCII-value representing) the second letter in the string, that is 'x'.

If you want the size of a string you can use the function strlen. The syntax is this:

int length = strlen(str);

where "str" is our char pointer that is pointing at the first char in our string (that is 'e'). strlen will return the length of the string, not including the NULL character '\0' that simply marks the end of the string. That means in our example, length will equal 7, since there are 7 letters in the word "example". If you want to extract the last letter of this string, now all you have to do is what we did before, but remember that indexing in C start at 0. This means that if you have a string of length 7, the last element of this string will be located at "index" 6. Thus, to get the last char of a string you have to do this:

char last = *(str+length-1);

or if you have not saved length to a variable of type int, you can do it like this instead:

char last = *(str+strlen(str)-1);

If you want a pointer, pointing to the last char of the string, you have to initialize a new char pointer and make it point to place (memory address) where the last char of "str" is located. By the same logic as before, this is given by the memory address of the char at "index" 6 of our original string "str". So you create a new pointer, and let that pointer point to this memory address like this:

char *last = str+strlen(str)-1;

Remember that you need to include the header file string.h at the top of your file like so:

#include <string.h>

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.