I need to make one 2D array in which one column store the pointer of some structure & another column store one 32 bit magic number.
how can i do it in 2D array.?
or any other method to keep track of this two columns info?
You can use:
// The struct that will hold the pointer and the magic number
struct data {
void *other_struct_ptr;
unsigned int magic_number;
};
// Declare my array
struct data array[N];
Where N is the size of your array. Now just fill your data into the array. For example:
array[0].other_struct_ptr = NULL; // I used NULL for simplicity
array[0].magic_number = 0xDEADC0DE;
array[1].other_struct_ptr = NULL;
array[1].magic_number = 0xCAFEBABE;
Define a struct like this:
struct data_t
{
void *pointer;
int magic_number;
};
Then use following array:
data_t values[100]; //100 is just for example
Or maybe you need such 2D array:
data_t values[100][100]; //100s are just for example
Related
I want to create the game 2048 in C, I have a few structs to achieve this:
typedef struct Board {
unsigned int size;
Cell ***cells;
} Board;
typedef struct Cell{
unsigned int value;
} Cell;
The structs are given as a homework, so I cannot change them
What I want to do now is initialize the Board and put one value into it:
Board* create_board(const unsigned int size){
Cell ***cell = (Cell***)malloc(sizeof(Cell *) * size * size);
Board *myboard = {size, cell};
Cell c1 = {2048};
myboard.cells[0][0] = c1; //I need help in this line
return myboard;
}
I am just learning C and pointers are still a little confusing for me, especially if there are 3 * in a row. How do I go about assigning values to a triple pointer array inside a struct?
Any help is appreciated, it was very hard for me to find information online about this kind of problem.
i have the following structure:
typedef struct Course {
int course_id;
char* course_name;
int prior_course_id;
StudentTree* students;
} Course;
and the following function i need to implement:
void createReport(FILE* courses[], int numOfCourses, FILE* studentFile, char* reportFileName
as you can see i get an array of FILE*, each cell contains different file pointer.
my intention is to create an array that each cell is Course* type, and initialize each cell with a Course struct containing the data read from the courses files.
what is the correct way to declare it inside the function?
do i need to dynamically allocate memory for it, or it can be done in compilation?
i've tried
Course* course_array[numOfCourses] = {NULL};
Course* course_array[numOfCourses] = NULL;
but it won't compile.
thanks for your help
You declare an array of structs the same way you declare an array of ints or FILE *s:
Type variableName[numberOfElements];
Before C99 (and barring compiler specific extensions), creating an array with a variable number of elements on the stack wasn't supported. So make sure that you are targeting the correct standard. In your case, assuming C99 support, the following should work:
Course *course_array[numOfCourses];
Because you intend to initialize each of the elements in the array, there is no need to zero them out.
You would then access the elements like this:
course_array[0] = malloc(sizeof(Course))
course_array[0]->course_id = 2;
/* etc. */
Now if you can't assume C99 support, things get a bit more tricky but not much:
Course *course_array = malloc(sizeof(Course *) * numOfCourses);
After that you can access course_array with the same array notation:
course_array[0] = malloc(sizeof(Course))
course_array[0]->course_id = 42;
/* etc. */
Once you're doing with the array, you'll need to make sure that you free any of the memory that you allocated:
for (i = 0; i < numOfCourses; i++) {
free(course_array[i]);
}
/* If you malloc'd course_array, then you need this too */
free(course_array);
Course* course_array[numOfCourses] = {NULL};
This is good, but it creates array of Course *. So you need to allocate memory for each pointer in course_array before accessing it.
Something like
course_array[0] = malloc(sizeof(Course));
course_array[0]->course_id = someid;
When you define the array in the first place, you shouldn't need to allocate memory. You're defining the array on the stack, and the elements of the array are just pointers.
I think what you should do is first define the array, and then initialize each element with a malloc call. For example:
Course* course_array[numOfCourses];
for(int i = 0; i < numOfCourses, i++) {
course_array[i] = (Course*)malloc(sizeof(Course));
My favorite way:
typedef struct {
int a;
char b;
float c;
}DATA;
//then use typdef'ed DATA to create array (and a pointer to same)
DATA data[10], *pData;
//then, in function, you can initialize the pointer to first element of array this way:
int main(void)
{
pData = &data[0];
return 0;
}
Your example code would look like this:
typedef struct {
int course_id;
char* course_name;
int prior_course_id;
StudentTree* students;
} COURSE;
//then in function:
COURSE course[numOfCourses]
C structure question
I have a list of words and their corresponding frequencies:
word 10
the 50
and 35
overflow 90
How should I hold this data in a structure? Should I use a two dimensional array? I should also note I have to sort them by their frequency, so I'm thinking an array of some sort then apply qsort, but I need to preserve the integers so if I use a char array I have to do back and forth casting
Possibly a struct:
struct WordInfo {
char *word;
int frequency;
};
Then you can make an array of these structs:
struct WordInfo words[128]; // whatever
And finally write a comparator function like this:
int word_compare(const void *p1, const void *p2)
{
struct WordInfo *s1 = p1;
struct WordInfo *s2 = p2;
return s1->frequency - s2->frequency;
}
If you know the maximum number of characters for the string which you are going to handle means use array and int data type inside a structure.
typded struct _data
{
char word[MAX_CHARS];
int freq;
}DATA;
...
DATA *d = malloc(sizeof(DATA) * n);
If you dont know the max characters of word go for pointer charater char *word;. In this case memory allocation will happen for each entry which will affect the performance and it will cause more fragmentation.
Its better to allocate a chunk of memory once rather than allocating small memory for n times.
You can also have an array of std::pair
Then you can run whatever sorting algorithm you want to sort the array based on the second element.
For example you will have:
std::pair myArray[size];
Define a structure type, something like
struct wordAndFrequency
{
char *word;
int count;
};
then take an array of struct wordAndFrequency and qsort that.
typedef struct Dict {
char *word;
int frequency;
} Dictionary;
Now create array of this structure object and use it as hashmap. When you come across a word, see if this word is there, increment the counter else add this word with count as 1.
Dictionary *dictionary=(Dictionary*)malloc(sizeof(Dictionary)*SIZE);
I am trying to assign an int pointer to an int array which is a member of a structre.
The structure is a member of another structure, which happens to be an array of structures. And this array of structures happens to be a member of another structure. And this last structure happens to be an element of an array of structures.
typedef struct s_ptxRowProperties
{
int lastPlotValue[134];
} ptxRowProperties;
typedef struct s_ptxRow
{
ptxRowProperties PtxRowProperties;
} ptxRow;
typedef struct s_workSpace
{
ptxRow PtxRow[100];
} workSpace;
Edit:
I allocate 1 of these behemoths like this:
WorkSpace[n] = (workSpace *) calloc(1, sizeof(workSpace));
I have tried the following incantations, to no avail:
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= (&WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue)[0];
int *x= WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;
int *x= *(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue);
int *x= (*WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue);
I believe the hypothetical million monkeys in a room for 100 years will have composed Hamlet before they can create the correct form for this. Any ideas?
You probably want
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0]);
This assumes that WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0] would reference the first element of the int array.
If WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue is the value you are interested in, than one would use & to get its address:
int *x = &WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;
I've searched stackoverflow and seen every combination of the words in my question, but not the question I have.
I have an array of ints, it happens to be a 2d array.
const int themap[something][something] = { {0, ...
I have a struct that I want to have a pointer to this array in my program
typedef struct {
int** mymap;
} THE_STRUCT
In my program I want to iterate over the values of the array through the struct's pointer, but my data seems to be corrupted if i try to access it through the . syntax
int value;
THE_STRUCT mystruct;
mystruct = (int**) themap;
...
//access the map data from mystruct's pointer?
value = mystruct.mymap[x][y];
//doesn't seem to return correct values
Taking the struct out of the picture the same exact function works if I directly use the array (as a global variable)
int value;
...
//access the map directly
value = themap[x][y]
//everyone is happy!
I would like to use the struct as in reality it will carry other information as well as the fact that I will need to be able to assign the pointer to other arrays with different data.
Your two-dimensional array is not the same as an int **. If you want to store a pointer to it inside the struct, you can do something like:
const int themap[something1][something2] = { {0, ...
typedef struct {
const int (*mymap)[something2];
} THE_STRUCT;
...
THE_STRUCT my_struct;
my_struct.mymap = themap;
...
int value = my_struct.mymap[x][y];
It is possible to use an int **, but it requires some effort:
const int themap[something1][something2] = { {0, ...
const int * themapPointerArray[something1] = {themap[0], themap[1], ..., themap[something1 - 1]};
typedef struct {
const int **mymap;
} THE_STRUCT;
...
THE_STRUCT my_struct;
my_struct.mymap = themapPointerArray;
...
int value = my_struct.mymap[x][y];
A multidimensional array int [][] and a double-indirect pointer int ** are two completely different things.
A multidimensional array is, to C, a one-dimensional array indexed in a different way. Say x is int [3][4]. Then, x contains 12 sequentially-packed elements, and x[1][2] is just the 6th element of that one-dimensional array.
A double-indirect pointer treated as a 2-dimensional array is an array of pointers to arrays. So, if y is int **, then y[1][2] means "the third element of the array pointed to by the second element of y".
You cannot therefore convert between int [][] and int **, since they just represent different things (your casting of int [][] to int ** causes the integers in the int [][] array to be treated as pointers, which will inevitably crash).
Instead, you can cast int [M][N] as int (*)[N] -- a pointer to an array of N-length arrays.