I am trying to allocate a block of memory, and store a list of structures without using multiple mallocs for each... this is just a generic example, I don't have the ... |
I'm trying to implement a ring buffer with the following struct
/*head, tail are indexes of the head and tail of ring buffer
*count is the number of elements; size is the ...
|
I am writing a program in C and I am trying to create these structs.
I have three of them:
- the first consisting of 2 ints and 2 chars,
- the second consisting of an ...
|
I have a struct which is a node, and another which is a list of these nodes. In the list struct, its an array of nodes, but instead of an array, ... |
Did you try it with '+1' ? Anyway, without reproducible example we can't help you much - you can try to step it through in debugger and see where the string that is supposed to be there corrupts, or put debug prints there for, e.g., program[0].cmd withing the loop to detect when it corrupts. |
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr; int count; ... |
mad_muppet Registered User Join Date Aug 2006 Posts 52 part 1 of 2 questions .. should I use malloc here? 2 arrays of structs? I have just finished writing my new mandelbrot set program and it compiles and runs .. sometimes though it segment faults when I try and increase the iteration count maximum and the escape value. I did change ... |
|
|
Code: #include #include #define STR_LENGTH 30 void memoryAlloc (int a); void display(struct book_info* a, int count); struct book_info { char author[STR_LENGTH]; char title[STR_LENGTH]; char pg[STR_LENGTH]; }; int main(int argc, char* argv[]) { int count; int i; fputs("Pease enter how many book to record: ",stdout); scanf("%d",&count); memoryAlloc(count); fflush(stdin); struct book_info* bookArray[count]; //variable cannot put in for (i = 0; i ... |
typedef struct { int roll; }REC; int main() { int i, n =3; REC *pt= malloc(n*sizeof(*pt)); printf("\n* * * * Enter roll no of students. * * * *\n"); for (i=0;i"); scanf("%d", &pt[i].roll); } printf("\n* * * * Entered roll no. * * * *\n"); for (i=0;i |
Congrats on your Sudoku program. I don't see any advantage to using a structure for the board. I've written two versions of Sudoku solvers - one with a 2D board, and one with a 3D board (using the deeper boards to hold the "possibles" for each square). Both worked with the standard 9 x 9 game boards. The 2D board was ... |
|
I've been working with pointers a bit, and I've made the following: Code: #include struct structurevariable{ char a[20]; }; int main() { int count; struct structurevariable * var[2]; //An array of 2 strucure pointers is created. for (count = 0; count < 2; count++) { /* The size of var[count] is determined. First a typecast is done to let malloc ... |
#include #include #define LINE_COUNT 10000 struct CAN_line { int cnt; int time; int identifier; int datalength; int db[8]; }; int main() { char buf[BUFSIZ], id_file[256]; char **id_file_array; char **id_array; struct CAN_line *linenr; int i; id_file_array = malloc(LINE_COUNT * sizeof *id_file_array); if ( id_file_array != NULL ) { for ( i = 0; i < LINE_COUNT; ++i ) { id_file_array[i] ... |
|
I hope he pointed out the syntax was type* p = malloc(n * sizeof p). In C, don't typecast malloc. EDIT: There also seems to be confusion in your code. You have one struct pointer. Using index notation is pointless because it will be out of bounds. What you probably should have done is have a pointer to a pointer. |
i could really use some help. i cannot figure out how create an array of part structs, that will be determined by two variables, rp and cp times each other. i have given sample values of rp and cp but they will be determined differently. same with rowsizer and colsizer. also i need to do it in c, not c++ here ... |