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 2 years ago.
Improve this question
How to declare variables for the 3 parameters associated with sampling the x-axis and give them values? How to declare arrays for 3 float arrays: x[], y[], and z[]?
You need to read about arrays usage in C. Declaring array of floats is same as declaring it for any other type. You can declare float arrays like this:
float x[] = {3.544, 5.544, 6.544, 6.544};
float y[] = {4.223, 21.12, 43.1, 4.3};
float z[] = {5.12, 34.5, 12.2, 3.5};
If you want to dynamically allocate it, you might want to use malloc for dynamic allocation.
Related
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?
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 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
What are the differences between these three pointers in C? I’m confused on how they differ.
int* arr1[8];
int (*arr2)[8];
int *(arr3[8]);
int* arr1[8];
arr1 is an array of 8 pointers to int.
int (*arr2)[8];
arr2 is a pointer to array of 8 ints.
int *(arr3[8]);
Same as arr1. The brackets are superfluous.
You may want to read Right-Left rule on how to read complex C declarations.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've seen certain functions declared in this way:
char* encipher(const char *src, char *key, int is_encode);
I don't understand this part:
char* encipher
What do the asterisks after the datatype mean?
This just means that the function returns a char *.
The asterisks after the data types mean that a pointer is expected, i.e.
char *src
means that src is a pointer to a char. Pointers are data types that contain addresses to instances of other data types, so a char* contains the address of a char. The first char* means that the function returns such a pointer.
But as others said, you may want to read a good textbook on C first.