Pointer to pointer in a structure [closed] - c

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.

Related

What's going to happend with these pointers? [closed]

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 4 years ago.
Improve this question
I've seen some code that I can't figure out exactly what's going on there, the piece of code is: I got char *s1, char *s2 from the function, and they've done this:
*s1 = *s2;
What's going on behind the sence with this? (in C, asci-c)
In C and C++ * can be used with the pointer of a variable to access and assign its value.
Here:
*s1 = *s2;
*s2 is accessing the value of the variable that is pointed by s2 pointer and then assigning this value to the variable that is pointed by s1. While = is an assignment operator used to assign a value to a variable.
Please visit this site for complete reference.

How should fields of a struct be accessed through a pointer to a pointer? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Say I have a function f(struct_t** p) that manipulates a struct s through p. f calls a function g(struct_t** p) that will reallocate s such that the caller of f will still be able to access s through *p after f returns. I can think of three ways to handle access to fields of s in f:
Assign struct_t* q = *p at the beginning of f for more readable access to s through q->field_name rather than (*p)->field_name, and reassign q to *p after each call to g(p).
Access fields of s using (*p)->field_name throughout f and sacrifice readability.
#define q to (*p) for the duration of f.
Which of these would be best? If none of them are ideal, how should fields of s be accessed in f?

char pointer value reassignment [closed]

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.

Pointers in C with arrays [closed]

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.

C - pointers and change address [closed]

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.

Resources