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;
}
Related
I managed to put the value in the pointer while in the function, However when i come back to the main i just dont get the values. Where am i wrong? sending parameters wrong? wrong allocation? Here's the code:
bool wc(int* nlines, int* nwords, int* nchars)
{
int lines=5,chars=6,words=7;
nchars = (int *) malloc(chars*sizeof(int));
*nchars = chars;
nlines = (int *) malloc(lines*sizeof(int));
*nlines = lines;
nwords = (int *) malloc(words*sizeof(int));
*nwords = words;
}
int main() {
int* chars; int* words; int* lines;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",chars,lines,words);
return 0;
}
If all you want to do is be able to set 3 int values inside a function then this is how I would so it.
#include <stdio.h>
#include <stdbool.h>
bool wc(int* nlines, int* nwords, int* nchars)
{
int lines=5,chars=6,words=7;
*nchars = chars;
*nlines = lines;
*nwords = words;
return true;
}
int main() {
int lines = 0;
int words = 0;
int chars = 0;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",chars,lines,words);
return 0;
}
If for some reason you must use pointers as shown in your example then this will do what you want.
#include <stdio.h>
#include <stdbool.h>
bool wc(int** nlines, int** nwords, int** nchars)
{
int lines=5,chars=6,words=7;
*nchars = malloc(sizeof(int));
**nchars = chars;
*nlines = malloc(sizeof(int));
**nlines = lines;
*nwords = malloc(sizeof(int));
**nwords = words;
return true;
}
int main() {
int* chars; int* words; int* lines;
int res = wc(&lines,&words,&chars);
printf("%d %d %d\n",*chars,*lines,*words);
free(chars);
free(words);
free(lines);
return 0;
}
As you can see this just means you need to add a bunch more * all over the place.
In C function input variables are passed by value, not reference. So when you assign them locally, the value in the caller scope is unaffected. E.g.
void foo(int a) {
a = 5;
}
int main() {
int b = 3;
foo(b);
// here, b is still 3
}
This is exactly what you are doing in your example, though your variables are not int, but int*.
If your input variable is a pointer though, you can change the memory that the variable points to, and this will obviously reflect in the calling scope. E.g.
void foo(int *a) {
*a = 5;
}
int main() {
int b = 3;
foo(&b);
// here, b is 5
}
In your case, you want to allocate pointers, so you want your function signature to be a pointer to a pointer. E.g.
void foo(int **a) {
*a = malloc(sizeof(int));
}
int main() {
int* b = NULL;
foo(&b);
// here, b is allocated to a valid heap area
free(b);
}
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);
}
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
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 to write a normal sum function and a reentrant one in C. I have to pass a int and it have to be addedd to a INIT_VALUE. In the reentrant function the main pass a int* to keep the state. How can i initialize this pointer on the first call? I have to initialize it in the fun, not in the main. Thanks
#include <stdio.h>
#ifndef INIT_VALUE
#define INIT_VALUE 0
#endif
int somma(int x){
static int val = INIT_VALUE;
val += x;
return val;
}
int somma_r(int x, int* saveptr){
// pointer initialize and sum
// return old_value ;
}
int main (){
int x;
int s;
int s_r;
int *stato;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}
With the function signature in your program (int somma_r(int x, int* saveptr)) you cannot initialize the pointer on the first call.
You probably need this (3 lines of your code modified):
...
int s = INIT_VALUE; // otherwise s will not be initialized
int s_r = INIT_VALUE; // otherwise s_r will not be initialized
int stato = INIT_VALUE; // state to be used with the somma_r function
...
s_r = somma_r(x, &stato);
...
somma_r function
int somma_r(int x, int* saveptr){
*saveptr += x;
return *saveptr;
}
Version with initialisation inside the somma_r function. This requires a modification of the signature of somma_r:
int somma_r(int x, int **saveptr){
if (*saveptr == NULL) {
*saveptr = malloc(sizeof(int));
**saveptr = INIT_VALUE;
}
**saveptr += x;
return **saveptr;
}
int main (){
int x;
int s = 0;
int s_r = 0;
int *stato = NULL;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,&stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}