Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this code that gives a pointer an address and print it, but why that does not work??
void main()
{
int *b = (int*) 32;
printf("%d\n",b[0]);
}
b[0] dereferences an array which points to memory you haven't allocated. The effects of doing this are undefined. You might get a value returned or your program may crash if address 32 isn't readable from your process.
int *b = (int*) 32;
above code assigns memory address 32 to this pointer, i don't think that is you want, you will get access denied error when you call printf,
hope the following codes is useful toyou
int a = 32;
int *b = &a;
printf("%d\n",b[0]);
//output 32
printf( "%d\n", &b);
// output b pointer address.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can a structure's member (which is a pointer) be accessed through another pointer? Let's say that *ptr is the pointer i want to use to access *time, which is the pointer that belongs to the structure. Is it correct if I write ptr->time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
No. If you have...
struct {
int *time;
} *ptr;
int v[10], i = 0;
...then you have to write *(ptr->time) = v[i]
If time is a pointer, being inside a struct change nothing to that. So if you want to access the int pointed by time, you have to deference it too.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I have the following code:
#include <stdio.h>
#include <stdlib.h>
int *p;
int main() {
int a = 4, b = 8;
p = &b;
//TODO: fill in the blank
printf("a = %ld\n", /*Fill in here */);
printf("b = %ld\n", /*Fill in here */);
return 0;
}
How can I print the value of 2 variables a and b using only p to access them?
In general, you cannot do this with current code /approach.
There's nothing in C standard that guarantees the memory allocation strategy for two or more independent variables, so the previous-next memory location tracking is not possible. There's no deterministic way to deduce the value of a, with only access to p.
In case you need to access values of more than one variables (of same type) from a single pointer, consider creating an array, where the elements are guaranteed to reside in contiguous memory location, so pointer arithmetic is meaningful and we can reach the previous-next element deterministically.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
int main()
{
int i=0;
char *b,*a="a(a+b))";
b=a;
printf("%s",b);
printf("%d",a);
while(a[i]!='\0')
{
a++;
i++;
}
*a="(a+b)";
printf("%s",a);
}
initialy i am assigning a value to that char pointer
after moving it to the end using null comparision
can i again assign a value to that char pointer?
Can i do like this?
what is wrong here? can anyone please explain??
The reason your third line is failing is because youre trying to de-refrence and reassign a value to a "string" value on the stack. ( which is very very bad, please never do this if it ever lets you )
If you do want it to work, the compiler has to create a new string value and reassign the pointer so you would have to change the code to this.
a = "(a+b)";
This will create a new value on the stack and reassign the pointer to the beginning of that string.
However if you are going to use strings like that. PLEASE use const char *. Its ultimately safer and saves you a lot of headaches.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
when you are declaring static variables in C,
say you wrote a program as such:
int *c;
void foo(){
c = (int *) malloc (10*sizeof(int));
c = &c[3];
*c = *&c[3];
}
What does it mean to have *c? I mean I understand that * means that it points to something but what does *c do?
and also, why do you need to cast (int *) the return value of malloc()?
when you are declaring static variables in C
Not related to this question, or atleast to the code you've shown.
but what does *c do?
Assuming your question related to the statement *c = *&c[3];, it refers to the object at address held by c.
why did you have to cast (int *) in front of malloc?
You should not. Please do not cast the return value of malloc() [and family].
Note: You code snippet is very poor and bad practice, most likely to be invalid. c = &c[3]; obvious memory leak.
I mean I understand that * means that it points to something but what does *c do?
The * does not always mean that it point to something.
Example:
If you write this:
int* c;
It means c is a pointer points to an int variable.
When you do like this:
int* c = &x;
*c = 5;
The second * is pointer c dereference.