so I need to use qsort() to sort an array that contains a structure
#include <stdio.h>
// =========
struct pair
{
int encounters;
};// pair{}
int compaireEncounters(const void*, const void*);
int main()
{
struct pair* working[5];
working[0]->encounters = 10;
working[1]->encounters = 3;
working[2]->encounters = 1;
qsort(working, 5, sizeof(struct pair), compareEncounters);
int i = 0;
while (i < 3)
{
printf("%d \n", working[i]->encounters)
i++;
}
}
int compaireEncounters(const void* av, const void* bv)
{
int a = ((struct pair*)av)->encounters;
int b = ((struct pair*)bc)->encounters;
return(a > b);
}
I am trying to get the output:
1
3
10
but instead i get a segmentation fault core dump.
What is the issue here?
You must assign pointers to valid buffers before dereferencing pointers.
In this case, working should be an array of the structure, not an array of pointers.
Also don't forget to initialize all elements to be sorted.
There are also more mistakes in your code:
qsort is used without including proper header (stdlib.h)
Undeclared compareEncounters is used in the main function.
A semicolon is missing after the printf() statement.
Undeclared bc is used in the compaireEncounters function.
Fixed code:
#include <stdio.h>
#include <stdlib.h>
// =========
struct pair{
int encounters;
};// pair{}
int compaireEncounters(const void* , const void*);
int main() {
struct pair working[5];
working[0].encounters = 10;
working[1].encounters = 3;
working[2].encounters = 1;
working[3].encounters = 334;
working[4].encounters = 42;
qsort(working, 5, sizeof(struct pair), compaireEncounters);
int i = 0;
while (i < 3) {
printf("%d \n", working[i].encounters);
i++;
}
}
int compaireEncounters(const void* av, const void* bv){
int a = ((struct pair*)av)->encounters;
int b = ((struct pair*)bv)->encounters;
return(a > b);
}
If you want to work with an array of pointers,
Allocate buffers and assign them before dereferencing.
Fix the element size for qsort().
Fix compaireEncounters to compare the pointers to the structure.
#include <stdio.h>
#include <stdlib.h>
// =========
struct pair{
int encounters;
};// pair{}
int compaireEncounters(const void* , const void*);
int main() {
struct pair* working[5];
working[0] = malloc(sizeof(*working[0])); working[0]->encounters = 10;
working[1] = malloc(sizeof(*working[1])); working[1]->encounters = 3;
working[2] = malloc(sizeof(*working[2])); working[2]->encounters = 1;
working[3] = malloc(sizeof(*working[3])); working[3]->encounters = 334;
working[4] = malloc(sizeof(*working[4])); working[4]->encounters = 42;
qsort(working, 5, sizeof(*working), compaireEncounters);
int i = 0;
while (i < 3) {
printf("%d \n", working[i]->encounters);
i++;
}
}
int compaireEncounters(const void* av, const void* bv){
int a = (*(struct pair**)av)->encounters;
int b = (*(struct pair**)bv)->encounters;
return(a > b);
}
Related
I'm trying to change the x value in this code but I'm getting segmentation fault.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int **x;
} Type;
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x[0] = &a;
return 0;
}
if you want an array of pointers to ints
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x = malloc(sizeof(int*) * 10)) ;// say we need 10
type->x[0] = &a;
return 0;
}
Here's a very basic example of what I'm trying to do (note that this segmentation faults)
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
int *bar;
} Foo;
Foo **fooPointers() {
Foo **test = (Foo**) malloc(sizeof(struct foo) * 3);
for(int i = 0; i < 3; i++) {
Foo *curr = *(test + i);
int *num = &i;
curr->bar = num;
}
return test;
}
int main() {
fooPointers();
return 0;
}
the goal is to create an array of pointers of Foo, give each element meaningful values, and then return the pointer array.
Is anyone able to point me in the right direction as to why this doesn't work and how I can accomplish this task?
#include <stdio.h>
#include <stdlib.h>
typedef struct foo
{
int *bar;
} Foo;
Foo **fooPointers()
{
Foo **test = malloc(sizeof(Foo*) * 3); // should be `sizeof(Foo*)`
static int k[] = {0,1,2}; // new array
for(int j=0;j<3;j++)
{
test[j] = malloc(3*sizeof(Foo)); // No need to cast output of malloc
}
for (int i = 0; i < 3; i++)
{
Foo *curr = *(test + i);
//int *num = &i;
curr->bar = &k[i]; // storing different addresses.
}
return test;
}
int main()
{
Foo **kk;
kk = fooPointers();
for(int i=0;i<3;i++)
{
printf("%d\n", *(kk[i]->bar)); //printng the values.
}
return 0;
}
The output is :
0
1
2
This simple code crashes (Segmentation fault) and I don't understand why. Seems like this [] operation does not work properly with an array of structures. Maybe somebody knows the reason behind this weird behavior.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 3
typedef struct{
int a;
char * b;
}qwe;
void foo ( qwe **out){
int i;
*out = (qwe*)malloc(SIZE*sizeof(qwe));
for (i=0;i<SIZE;i++){
out[i]->a = i;
out[i]->b = strdup("Hello");
}
}
int main() {
int i = 0;
qwe *p = NULL;
foo(&p);
for (i=0;i<SIZE;i++)
printf("Int: %d, str: %s \n",p[i].a , p[i].b);
}
With the declaration qwe *p = NULL; you are not declaring an array of pointers but of the actual struct.
But in foo you are dereferrencing the arrays content as pointer and that is not correct.
Your approach can be rewritten like this:
for (int i = 0; i<SIZE; i++) {
(*out)[i].a = i;
(*out)[i].b = _strdup("Hello");
}
Another approach that makes the dereferrency better readable (in my opinion) would be to pass a reference to the array:
void foo1(qwe *& out) {
out = (qwe*)malloc(SIZE * sizeof(qwe));
for (int i = 0; i<SIZE; i++) {
out[i].a = i;
out[i].b = _strdup("Hello");
}
}
I have this code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
void ft_ultimate_ft(int *********nbr)
{
printf("%d", *********nbr);
}
int main(){
/*Start of problem*/
int *a;
int **b = &a;
int ***c = &b;
int ****d = &c;
int *****e = &d;
int ******f = &e;
int *******g = &f;
int ********h = &g;
int *********i = &h;
/*end of problem*/
*********i = 42;
ft_ultimate_ft(i);
return 0;
}
I need to include pointer-to-pointer declaration in the loop (for example, while). It's needed to decrease number of declarations.
I am assuming that I have properly understood you question, which is to create a multiple pointer to a number using a loop and then assign value to it.
I wrote a piece of code that partly completes your requirement but still need to know how many layers there are after the loop.
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
void ft_ultimate_ft(int *********nbr)
{
printf("%d", *********nbr);
}
int main() {
int t = 10;
int n = 8;
void * p = &t;
for (int i = 0; i < n; i++)
{
void* * s = (void **)malloc(sizeof(void*));
*s = p;
p = s;
}
/*end of problem*/
int *********real = (int *********)p;
*********real = 42;
ft_ultimate_ft(real);
return 0;
}
Which outputs 42
The clean up part of the program is not written
PS. In your code, the pointer a is indeterminate and I do not think that your original code can work properly.
I have the following code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct Example
{
uint16_t a;
uint16_t b;
} ExampleStruct;
void derp(struct Example * bar[], uint8_t i)
{
uint8_t c;
for(c = 0; c < i; ++c)
{
bar[c]->a = 1;
bar[c]->b = 2;
}
}
int main()
{
struct Example * foo;
uint8_t i = 3;
foo = malloc(i*sizeof(ExampleStruct));
derp(&foo, i);
free(foo);
return 0;
}
I get segfaults and all debuggers tell me that code stopped working due to
bar[c]->a = 1;
I tried to rearrange this into all of the following
(*bar)[c]->a = 1;
(*bar[c])->a = 1;
bar[c].a = 1;
(*bar)[c].a = 1;
and with no success. What am I doing wrong? I don't understand why is this failing, and I don't understand why the addresses of bar[0], bar[1] and bar[2] are so far away from each other, when each just takes 2 bytes.
There's no need to pass &foo. Keep it simple:
// In a function declaration, it's (almost) always a pointer, not an array.
// "struct Example bar[]" means *exactly* the same thing in this context.
void init(struct Example * bar, int n) {
int i;
for (i = 0; i < n; ++i) {
bar[i].a = 1;
bar[i].b = 2;
}
}
int main() {
int n = 3;
struct Example * foo = malloc(n*sizeof(struct Example));
init(foo, n); // passes the address of the array - &a[0] - to init
printf("The second element is {%u, %u}\n", foo[1].a, foo[1].b);
free(foo);
return 0;
}
output:
The second element is {1, 2}
Some changes were required since you were trying to pass array of objects:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
typedef struct Example
{
uint16_t a;
uint16_t b;
} ExampleStruct;
void derp(struct Example * bar[], uint8_t i)
{
uint8_t c;
for(c = 0; c < i; ++c)
{
bar[c]->a = 1;
bar[c]->b = 2;
}
}
int main()
{
struct Example * foo[3];
uint8_t i = 3, c;
for(i = 0; i < 3; i++)
foo[i] = malloc(sizeof(ExampleStruct));
derp(foo, i);
for(c = 0; c < i; ++c)
{
printf("\n%" PRIu16 " %" PRIu16 ,foo[c]->a,foo[c]->b);
}
for(i = 0; i < 3; i++)
free(foo[i]);
return 0;
}
struct Example * foo; can hold a single pointer to an object of type struct Example. While struct Example * bar[] can hold an array of pointers to objects of type struct Example.
In your original program, this will seg fault when c is greater than 0 since you did not allocate any pointers to an object of type struct Example.
bar[c]->a = 1;
bar[c]->b = 2;
For static objects:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
typedef struct Example
{
uint16_t a;
uint16_t b;
} ExampleStruct;
void derp(struct Example bar[], uint8_t i)
{
uint8_t c;
for(c = 0; c < i; ++c)
{
bar[c].a = 1;
bar[c].b = 2;
}
}
int main()
{
struct Example foo[3];
uint8_t i = 3, c;
derp(foo, i);
for(c = 0; c < i; ++c)
{
printf("\n%" PRIu16 " %" PRIu16 ,foo[c].a,foo[c].b); //accessing in main
}
return 0;
}