When we declare a pointer to any integer say we declare it like
int i=5;
int *p=&i;
We use *p to get the value 5 and if we want the address we use p without asterisk sign to get the address. But in case of character array, when we say
char *str="HELLO";
We simply use str to get "HELLO" as output in printf function. Like this,
printf("%s",str);
Here we used str without asterisk.
Why aren't we getting an address and instead getting "HELLO" as output here while we get address in case of pointers to integers when we use the pointer variable without asterisk?
Okay first of all int *p=5; is wrong! you cant do that in the declaration of a pointer. at That time the pointer expects an adress. You should do int *p; and then *p = 5; to assign the value to it.
printf("%s",str); // prints the value of the variable, because its a pointer.
printf("%s",*str); // the * will cause to a segmentation fault
int *p // the * there is just used to tell the compiler that you are declaring a pointer of type integer.
/* another scope after declaring your pointer */
*p //is used to get value of a pointer variable.(dereference it)
&p //is used to get the adress of a pointer variable.
p //contains an adress.
Related
In the example below, I pass a char into a function as a pointer. When the function prints out the memory address of the char, it is a different address. Am I dealing with two different variables in this case? Isn't that the same result as if I didn't use a pointer in the function argument (same as pass by value)?
char a = 'a';
printf("a=%p\n", &a);
showString(a);
//function
void showString(char *c){
c='b';
printf("c=%p\n", &c);
}
c is assigned the value 'b'. But if I check a after the function call, it still has the value 'a'. How does the above need to change so the value assigned in the function carries over outside of the function?
You are not using the pointer correctly: rather than assigning a new value to the object the pointer points to, you assign the pointer itself.
The call should use the address-of operator, and the assignment should be with the dereference operator:
showString(&a);
printf("a=%p\n", (void*)&a);
...
void showString(char *c){
*c='b';
printf("c=%p\n", (void*)c);
}
Now both printouts will produce the same address, and the value of char a in the calling function should change.
Note 1: The reason the compiler allowed c = 'b' assignment is somewhat odd: 'b' is considered a character constant, char is considered an integral type, and C lets you assign integers to pointers with the assumption that you know what you are doing better than the compiler.
Note 2: When you print a pointer with %p, and the pointer type is neither void* nor char*, a cast to void* is required. You are printing char*, so it can go without a cast, but I added the cast anyway, in case you want to try this out with pointers of a different type.
Your function should look like
//function
void showString(char *c) {
*c = 'b';
printf("c=%p\n", &c); //<---to print the address of C
printf("c=%c\n", *c); //<---to print the value that C is pointing
}
and when you call it you should pass the Address of a
char a = 'a';
printf("a=%p\n", &a);
showString(&a); //<---&a and not a
Here is a program, I write it to output all the characters of a string one by one. But I also print the address of individual blocks of the array. The problem is addresses for all blocks are same. Why?
Does someone know?
#include<stdio.h>
int main()
{
char enter[]="Kinsman";
char *ptr;
ptr=enter;
int i=0;
while(*ptr!='\0')
{
printf("%c%p\n",*ptr,&ptr);
ptr++;
for(i=0;i<=100000000;i++);
}
return 0;
}
Because you print the address of the actual pointer.
When you use &ptr you get the address of the actual pointer and not the address if points to. Remove the ampersand (address-of operator &) so you have only ptr.
You are printing the address of the pointer, not the value of the pointer
Try
printf("%c%p\n",*ptr, static_cast<void*>(ptr));
(https://stackoverflow.com/a/18929285/259)
ptr is a pointer and it is also a variable in stack that has an address. This is fixed, while what it points to is varying by ptr++, thus you've to print the pointed-to value and not the address of the pointer itself.
printf("%c%p\n",*ptr, (void*)ptr);
// ^ remove & , and add void*
Hi please advise me on the following output:
main()
{
char ***x = "jjhljlhjlhjl";
char **q = *x;
printf("x:%s\n",x);
printf("q:%s\n",&q);
}
Output:
x:jjhljlhjlhjl
q:jjhl
Why is q doesn't print the whole of x ?
Your program invokes undefined behavior, so there are not really any limitations on what it may output.
char ***x = "jjhljlhjlhjl";
Although x is a pointer to a pointer to a pointer, it is assigned the address of a string literal.
char **q = *x;
q is a pointer to a pointer, and is assigned the result of dereferencing x. Since x is actually pointing to an object of incompatible type, the result of derferencing it is undefined.
If we pretend like this is supposed to work, then *x is now a pointer to a pointer, and so it might treat the sizeof(char **) bytes of the string literal as if it were an address and assign that value to q.
printf("x:%s\n",x);
Since x is a pointer, that pointer value is passed to printf(). Since the %s is provided, the pointer value is treated like a string. Since x was assigned the address of a string literal, that string is what gets printed.
printf("q:%s\n",&q);
The address of q is passed to printf(). Since the %s is provided, the pointer value is treated like a string. However, derferencing the contents of that pointer is actually sizeof(char **) bytes of the string literal. There is no guarantee that the bytes read will be properly NUL terminated, so it is just happenstance that something got printed at all.
The issue is the string "jjhljlhjlhjl" is only a char *, whereas you assign it to a char ***. When you print x, it gets interpreted as a char * by printf, which winds up being the correct string.
As for printing &q as a string address, you're effectively printing the contents of q. When you dereference x, you get a char **, which in your case, is 4 bytes. But because x actually points to text data, *x will grab the first four bytes (i.e. characters) of your string. The fact that it's printing only four characters is pure chance.
I've got a function which receives a pointer to a string.
Now I want to duplicate this pointer, so I could run on the string with one pointer, and have another pointer which saves the beginning of the string. I hope my question is clear, I don't want to create a pointer to the pointer (which points to where the first pointer points and changes with it), I want a pointer which will point to the address of which the first pointer points.
I tried many different ways but none of them worked, this is my latest try, I keep getting
"initialization makes pointer from integer without a cast"
int move_chars (char *str, int start, int end, int dist){
int strsize = end - start + 1;
char *p = *str;
Since p is of type char * and str is also of type char *, you just need to do:
char *p = str;
To make p point to the same memory location as str.
Your code:
char *p = *str;
Attempts to copy into p the value of the first char at the memory location that str points to, and since p is expecting another memory location and not a char, you get an error.
Your error says that "initialization makes pointer from integer without a cast". This is telling you what's happening: The compiler thinks you are manually pointing p to a specific memory location, using the value of the char *str returns as an integer. This is technically doable, but you are required to manually cast the integer into a pointer, just so it's clear you're doing what you intended, and it's not a bug like in your case.
Use
char *p = str;
to declare a pointer to a char and initialise it with the value of another char pointer.
char *p = str
This will declare a pointer to character and makes p point to the memory location where str points
#include <stdio.h>
int main(void){
char *p = "Hello";
p = "Bye"; //Why is this valid C code? Why no derefencing operator?
int *z;
int x;
*z = x
z* = 2 //Works
z = 2 //Doesn't Work, Why does it work with characters?
char *str[2] = {"Hello","Good Bye"};
print("%s", str[1]); //Prints Good-Bye. WHY no derefrencing operator?
// Why is this valid C code? If I created an array with pointers
// shouldn't the element print the memory address and not the string?
return 0;
}
My Questions are outlined with the comments. In gerneal I'm having trouble understanding character arrays and pointers. Specifically why I can acess them without the derefrencing operator.
In gerneal I'm having trouble understanding character arrays and pointers.
This is very common for beginning C programmers. I had the same confusion back about 1985.
p = "Bye";
Since p is declared to be char*, p is simply a variable that contains a memory address of a char. The assignment above sets the value of p to be the address of the first char of the constant string "Bye", in other words the address of the letter "B".
z = 2
z is declared to be char*, so the only thing you can assign to it is the memory address of a char. You can't assign 2 to z, because 2 isn't the address of a char, it's a constant integer value.
print("%s", str[1]);
In this case, str is defined to be an array of two char* variables. In your print statement, you're printing the second of those, which is the address of the first character in the string "Good Bye".
When you type "Bye", you are actually creating what is called a String Literal. Its a special case, but essentially, when you do
p = "Bye";
What you are doing is assigning the address of this String literal to p(the string itself is stored by the compiler in a implementation dependant way (I think) ). Technically address to the first element of a char array, as Richard J. Ross III explains.
Since it is a special case, it does not work with other types.
By the way, you should likely get a compiler warning for lines like char *p = "Hello";. You should be required to define them as const char *p = "Hello"; since modifying them is undefined as the link explains.
As to the printing code.
print("%s", str[1]);
This doesnt need a dereferencing operation, since internally %s requires a pointer(specifically char *) to be passed, thus the dereferencing is done by printf. You can test this by passing a value when printf is expecting a pointer. You should get a runtime crash when it tries to dereference it.
p = "Bye";
Is an assignment of the address of the literal to the pointer.
The
array[n]
operator works in a similar way as a dereferrence of the pointer "array" increased by n. It is not the same, but it works that way.
Remember that "Hello", "Bye" all are char * not char.
So the line, p="Bye"; means that pointer p is pointing to a const char *i.e."Bye"
But in the next case with int *
*z=2 means that
`int` pointed by `z` is assigned a value of 2
while, z=2 means the pointer z points to the same int, pointed by 2.But, 2 is not a int pointer to point other ints.So, the compiler flags the error
You're confusing something: It does work with characters just as it works with integers et cetera.
What it doesn't work with are strings, because they are character arrays and arrays can only be stored in a variable using the address of their first element.
Later on, you've created an array of character pointers, or an array of strings. That means very simply that the first element of that array is a string, the second is also a string. When it comes to the printing part, you're using the second element of the array. So, unsurprisingly, the second string is printed.
If you look at it this way, you'll see that the syntax is consistent.