If condition inside the findset is not giving correct comparison. It is always shows not equal.
Where did I make a mistake?
#include<stdio.h>
struct node {
struct node *p;
int rank;
};
void makeset(struct node x) {
x.p = &x;
x.rank = 0;
}
void findset(struct node x) {
if (x.p == &x) {
printf("It worked bro\n");
}
}
int main() {
int nv, ne;
nv = 4;
ne = 5;
for (int i = 0; i < nv; ++i) {
struct node i;
makeset(i);
findset(i);
}
}
void findset(struct node x)
{
if (x.p == &x)
{
that x is a copy of the one passed in... x is always brand new and &x wont match any previous address.
Note that this is true not just of structs but all values...
int i = 3;
void f( int j)
{
// &j != &i
// j == i
// j and I have the same value, but different address
}
int main()
{
f(i);
}
There is no way your code will ever work. You ignore the fact that C uses pass by value -- meaning that when you pass a stuct node as a parameter to your function, (e.g. void makeset(struct node x)) the function receives a copy of the struct with its very own (and very different) address than the original struct in main().
So when you pass your struct to void findset(struct node x), there is no way if (x.p == &x) will ever test true -- because there is no requirement that the address of the copy of x received by void findset(struct node x) and stored in p is the same address as the copy of x that got passed to void makeset(struct node x).
Your second problem is your declare the same struct i 4 times in:
for (int i = 0; i < nv; ++i)
{
struct node i;
makeset(i);
findset(i);
}
You already have a variable i declared in your for loop. When you make the declaration struct node i;, you are not creating struct node 0;, struct node 1; ..., you are simply creating struct node i; 4 times. (no variable can be named a number or begin with a number in C) The i in your struct node declaration shadows the loop counter declaration of i. Add the warning -Wshadow to your compile string as a standard part of your compiler warnings.
What you need to do is declare an array of struct node if your want to be able to fill your nodes separately. For the purpose of this example, you could declare a struct with the same name 4-times - but that is rather limited to the use within your for loop (as your struct is declared within the for loop block, it is destroyed (the stack memory released for reused) each and every iteration of the loop. To prevent that from occurring, you can declare an array of 4 struct node before you enter your loop, e.g.
#include <stdio.h>
#define NNODES 4 /* if you need a constant, define one */
...
int main (void) {
struct node nodes[NNODES] = {{ .p = NULL }};
Next, to have any chance of findset ever being able to compare the original address of the struct node you pass as a parameter, you must pass the address of the struct, instead of the struct itself. That way, even though the function receives a pointer to a struct node, the value contained is the original address of the struct from the calling function (main() here)
So you need to change both function declarations to:
void makeset (struct node *x)
and
void findset (struct node *x)
You will also need to use the -> operator to access the members instead of the '.' operator.
Putting all the pieces together, you could do something similar to the following:
#include <stdio.h>
#define NNODES 4 /* if you need a constant, define one */
struct node {
struct node *p;
int rank;
};
void makeset (struct node *x)
{
x->p = x;
x->rank = 0;
}
void findset (struct node *x)
{
if (x->p == x)
printf ("It worked bro\n");
}
int main (void) {
struct node nodes[NNODES] = {{ .p = NULL }};
for (int i = 0; i < NNODES; i++) {
makeset (&nodes[i]);
findset (&nodes[i]);
}
}
(note: how the address of each struct node is passed to each function in main(), e.g. makeset (&nodes[i]);)
Example Use/Output
$ ./bin/struct_ptr_addr
It worked bro
It worked bro
It worked bro
It worked bro
It works now bro...
If you did really intend to declare a single stuct node in your for loop that is created and destroyed each iteration, then it works the same for a single struct as well -- you still have to pass the address of the struct to each function, e.g.
#include <stdio.h>
#define NNODES 4 /* if you need a constant, define one */
struct node {
struct node *p;
int rank;
};
void makeset (struct node *x)
{
x->p = x;
x->rank = 0;
}
void findset (struct node *x)
{
if (x->p == x)
printf ("It worked bro\n");
}
int main (void) {
for (int i = 0; i < NNODES; i++) {
struct node somenode = { .p = NULL };
makeset (&somenode);
findset (&somenode);
}
}
(the output is the same)
Hopefully, you now understand that when you pass a variable as a parameter, the called function receives a copy of the variable holding the original value. And, if you want to pass the address of the variable, then you have to pass the address as a pointer to insure that that original address is available within the called function (and any changes made visible back in the caller).
As the other poster said, you're passing in the value instead of the actual variable. The solution here is one of the reasons pointers are so powerful, you can just pass the pointer for the actual variable you want to change. For example:
void foo(){
int i = 8;
bar(&i);
}
int bar(int* i){
return *i * 2;
}
That should work better for you
Related
Generally, I know that a pointer stores the memory address of another value located in computer memory, for example:
int firstvalue= 5
int * p1;
p1 = &firstvalue; // p1 = address of firstvalue
What happens if we define an operation like the following in a linked list? Does *current=*list means that the value pointed to by current equals to the value pointed to by list? And what does it mean if we define ecur=current?
int function(struct list_t **list){
struct list_t *ecur=NULL;
struct list_t *current=*list;
ecur=current;
}
Update:
What does it do *list=remove(*list, param1, param2)? And why is that?
remove is a function that returns a modified list of list.
Update 2:
Why do we need to define a pointer to pointer in order to modify the list? Is *list a pointer to pointer?
The variable list is a pointer to a pointer to a struct list_t. If we (just as an example) assume that the struct is placed at address 2000 and that the unnamed pointer is at address 1000 it will look like this:
Then you have the initialization that adds two new variables. Both as pointer to a struct list_t.
struct list_t *ecur=NULL;
struct list_t *current=*list;
So the picture now becomes:
Notice that current got the same value as the "some-pointer" in the middle because it is *list that was assigned to current.
Then you have the assignment:
ecur=current;
which means that ecur gets the same value as current and gives the picture:
Update: What does it do *list=remove(*list, param1, param2) ?
It changes the value of the "some-pointer" in the middle of the picture. This is for instance needed if the remove function removes the first element in a linked list.
Why do we need to define a pointer to pointer in order to modify the list? Is *list a pointer to pointer?
Remember that C passes all function arguments by value - the formal argument in the function definition is a different object in memory from the actual argument in the function call. For example:
void swap( int a, int b )
{
int tmp = a;
a = b;
b = tmp;
}
void foo( void )
{
int x = 1;
int y = 2;
swap( x, y );
}
a is a different object in memory than x, and b is a different object in memory than y, so swapping a and b has no effect on x and y. In order to swap the values of x and y, you must pass pointers to them:
void swap( int *a, int *b )
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void foo( void )
{
int x = 1;
int y = 2;
swap( &x, &y );
}
The expression *a is the same as x, so writing to *a is the same as writing to x. Same for *b and y.
So, in order for a function to write to a parameter, you must pass a pointer to that parameter:
void foo ( T *arg )
{
*arg = new_value(); // writes a new value to the thing arg points to
}
void bar( void )
{
T var;
foo( &var ); // write a new value to var
}
This is true for any non-array type T. Let's replace T with a pointer type P *:
void foo( P **arg )
{
*arg = new_value(); // write a new *pointer* value to the thing arg points to
}
void bar( void )
{
P *var;
foo( &var ); // write a new pointer value to var
}
The semantics are exactly the same - all that's changed is the type.
If a function has the potential to modify a list * object (say pointing it at a new list head), then you must pass a pointer to that list * object:
void add_node( struct list_t **list, struct list_t *node )
{
if ( !*list || (node->value < (*list)->value) ) // make node new head of list
*list = node;
else
// add node somewhere else in the list
}
int main( void )
{
struct list_t *list = NULL;
...
struct list_t *node = newNode( value );
add_node( &list, node );
...
}
TYPE *p = ptype /*variable of type: TYPE * */;
is not an assignment. It's an initialization, which for an auto-matic (=on-the-stack) p can be rewritten as:
TYPE *p;
p = ptype;
(not TYPE *p; *p=ptype; /*would be a type error*/)
In terms of your example:
struct list_t *current=*list;
sets where current will point to (the same place as what *list points to (*list is also a pointer because list is a doubly-indirect pointer)) without doing anything whatsoever with what current will point at (*current) after the initialization.
All of this is just conceptual, though. Your function doesn't have any externally visible effects so an optimizing compiler should completely delete its body.
I had a similar knot in my head with this post. I'd like to rearrange your function a bit, so it's easier to understand what's going on:
int function(struct list_t **list)
{
struct list_t *current = *list;
struct list_t *ecur = current;
}
If we call this function with an element foo we essentially get this:
struct list_t foo = { .data = "foo" };
struct list_t *bar = &foo;
struct list_t **list = &bar;
struct list_t *current = *list;
struct list_t *ecur = current;
We have five declarations and five assignments. For better readability, I'll write everything down without declarations:
foo = { .data = "foo" };
bar = &foo;
list = &bar;
current = *list;
ecur = current;
Now, let's walk through it:
foo is a struct. It contains the above data-field.
bar is a pointer to struct. It contains the address of foo
list is a pointer to a pointer to struct. It contains the address of bar
current is a pointer to struct. It contains the contents of the contents of list, which is the address of foo
ecur is a pointer to struct. It's identical to current and contains the address bar
In the end we can simplify the whole example to this:
struct list_t foo = { .data = "foo" };
struct list_t *ecur = &foo;
What does it all mean?
list: Because list is a pointer to a pointer you are able to modify bar to point to something completely different, by de-referencing it (*list = ...)
current/ecur: that's what bar originally pointed too. By de-referencing you could change the data-field itself ((*ecur).data = "banana" or better ecur->data)
I hope I could clarify things and didn't make it worse ;)
Why do we need to define a pointer to pointer in order to modify the
list?
Let me add a complete program, albeit short, to illustrate it better. It defines a simply linked list and builds it while keeping it ordered. Yes, I know it would be easier to simply call qsort(), but I want to demonstrate how adding one level of indirection —the pointer to pointer— allows to insert elements smoothly, without testing for special cases.
// C pointer exercise: sort arguments
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct list
{
char *arg;
struct list *next;
};
int main(int argc, char *argv[])
{
// pointer to base, running pointer and pointer to pointer
struct list *base = NULL, *p, **pp;
for (int i = 1; i < argc; ++i)
{
struct list *new_entry = malloc(sizeof(struct list));
if (new_entry)
{
new_entry->arg = argv[i];
// find where to insert new entry
for (pp = &base; *pp; pp = &(*pp)->next)
if (strcasecmp(new_entry->arg, (*pp)->arg) < 0)
break;
// insertion in a simply linked list
new_entry->next = *pp;
*pp = new_entry;
}
}
// display and cleanup
for (p = base; p;)
{
struct list * tmp = p->next;
puts(p->arg);
free(p);
p = tmp;
}
return 0;
}
I'm trying to solve this codewars kata
Basically I need to wite a programe that spits out an array/list of numbers from a perticular range (of numbers) which have k primes multiplicatively.
countKprimes(5, 500, 600) --> [500, 520, 552, 567, 588, 592, 594]
Now my program "works" as in it can print the results correctly, but if I put it in codewars' answer area (without main of course), it just runs forever.
"Error code SIGKILL : Process was terminated. It took longer than 12000ms to complete"
This is the codewars template
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// In the preloaded section are some functions that can help.
// They can be used as a small library.
// There is no file to include, only the templates below.
struct node {
int data;
struct node *next;
};
struct list {
size_t sz;
struct node *head;
};
struct list* createList();
// push data at the head of the list
void insertFirst(struct list* l, int data);
struct list* reverse(struct list* l);
void listFree(struct list* l);
// functions to write
struct list* kPrimes(int k, int start, int nd)
{
// your code
}
And this is my code
#include <stdio.h>
struct list{
int a[600];
};
int smallestPrimeFactor(int number){
int x;
for(x = 2; x < number; x++) {
if(number % x == 0) {
return x;
}
}
return number;
}
int primefactors(int ofnumber){
static int counter = 0;
int tempcounter = counter;
int nextnumber = ofnumber/smallestPrimeFactor(ofnumber);
if(nextnumber != 1) {
if(ofnumber >= nextnumber) {
counter++;
primefactors(nextnumber);
}
}
return (counter - tempcounter) + 1;
}
struct list kPrimes(int k, int start, int nd){
int x, g = 0;
struct list ls;
for(x = start; x < nd; x++){
if(primefactors(x) == k){
ls.a[g] = x;
g++;
}
}
return ls;
}
int main(int argc, int **argv){
int p = 5, s = 500, e = 600;
int j = 0;
while(kPrimes(p, s, e).a[j] != '\0'){
printf("%d\n", kPrimes(p, s, e).a[j]);
j++;
}
}
I think the culprit here is
struct list{
int a[600];
};
Maybe while reading the array, the test file is overshooting a's index past '\0'.
I thought of a way of solving that by making a a pointer to integer but doing int *a; prints out nothing.
I know there are more than one way of returning an array. Using referance, using a static array, passing an array as argument, etc. But I want to solve this codewars' way. Think it'll be a nice learning experience.
So, how should I be using
struct node {
int data;
struct node *next;
};
struct list {
size_t sz;
struct node *head;
};
to solve the problem?
You should not bother about the structure themselves, you should simply use the provided functions:
struct list* kPrimes(int k, int start, int nd){
int x, g = 0;
struct list *ls = createList(); // 1. Create the list.
// 2. Maybe check if ls != NULL...
for(x = start; x < nd; x++){
if(primefactors(x) == k){
insertFirst(ls, x); // 3. Insert at the beginning.
g++;
}
}
struct list *rls = reverse(ls); // 4. Reverse the list.
listFree(ls); // 5. Free the original list.
return rls; // 6. Return the reversed list.
}
Since the functions reverse is not documented, I can only guess that it creates a new list without modifying the old one, which is why you need to free it after.
The createList(), insertFirst(), reverse(), and listFree() functions, as well as the function that consumes the return value of your function are all provided to you, and they all work with the types struct list and struct node. How, then, do you imagine it could work if you try to use a differently-defined struct list than those existing functions use?
So yes, you should be using the struct node and struct list types provided to you -- and the handy functions for manipulating them -- rather than defining different structure types with the same tags.
I have a function called SemCreate that takes a name an int and a pointer as parameters. I want the pointer to point to a new struct and I want to return an int, 0 if it went okay.
int P1_SemCreate(char *name, unsigned int value, P1_Semaphore *sem){
USLOSS_Console("Create a semaphore\n");
if(!verifyKernel()) {
//USLOSS_Console("ERROR: Not currently in Kernel mode\n");
USLOSS_Halt(1);
}
if(numSems>= P1_MAXSEM){
//USLOSS_Console("ERROR: MAX semaphore already exist\n");
return -2;
}
if(name == NULL){
return -3;
}
interruptsOff();
int i;
for (i = 0; i < P1_MAXSEM; i++) {
if (semTable[i].inUse == 0) {
semTable[i].value = value;
semTable[i].list = NULL;
semTable[i].name = malloc(sizeof(char));
strcpy(semTable[i].name, name);
semTable[i].inUse = 1;
semaphore *temp = NULL;
temp = malloc(sizeof(semaphore));
temp->value = value;
temp->list = NULL;
temp->name = malloc(sizeof(char));
strcpy(temp->name, name);
*sem = temp;
break;
}
}
numSems++;
interruptsOn();
return 0;
}
Right now the pointer is fine within the function but once I return the pointer is null
EDIT: The array semTable is an array of semaphores
typedef struct semaphore{
int value;
char * name;
struct node *list;
int checkPoint;
int inUse;
}semaphore;
typedef struct PCB {
USLOSS_Context context;
int (*startFunc)(void *); /* Starting function */
void *startArg; /* Arg to starting function */
int pid;
int status;
int killedStatus;
int state;
int priority;
char name[50];
int parentPID;
int numOfChild;
int blockFlag;
struct sempahore *blockSem;
char *stack;
struct node *children;
struct node *zombiList;
int cpuTime;
int startTime;
struct semaphore *childSem;
} PCB;
typedef struct node {
PCB *pcb;
struct node *next;
} Node;
Your question is not perfectly clear as to what you are trying to do. So, this answer addresses the following general topics in hopes they will assist:
1) Passing the address of a struct via a function, changing the values of the struct members, and accessing changed values in calling function. (It is not the same as the one you show, but illustrates what you want to do.)
2) Creating instances and pointers to instances of a struct, then initializing.
3) Contains link to tutorial on self referencing structs. (as you are doing in your posted code)
typedef struct {//struct definition created in global space, typedef'ed
char line[80]; //to read in the line
int slot;
char type[20]; //long enough for any types listed
int position;
}SLOT;
int modifyStruct(SLOT *slot);//prototype of demonstration function
int main(void)
{
SLOT s;//create instance of SLOT local to main
int ret = modifyStruct(&s);pass address of SLOT instance to change member values
printf("Line: %s\n", s.line);//show changed values
printf("Slot: %d\n", s.slot);
printf("type: %s\n", s.type);
printf("position: %s\n", s.position);
return 0;
}
int modifyStruct(SLOT *slot)
{
strcpy(slot->line, "lineA");
slot->slot = 2;
strcpy(slot->type, "large");
slot->position = 20;
return 0;
}
EDIT - To address question in comment asking how to set a struct pointer to point to a struct.
First, it appears from viewing the code you have posted, that you are using self referencing structs. (i.e. a struct that contains a member that is a pointer instance to itself) Here is a link to a good tutorial dealing with Linked Lists in C, which use self referencing structs.
Regarding you comment: _I guess I should have made it more clear. P1_Semaphore is different to semaphore. I need P1_semaphore to point to the semaphore_.:
If P1_Semaphore is different than semaphore, then one should not be set to point to the address of the other. And your compiler will not let you do that anyway.
As I stated in the comments, a struct pointer should only point to a place in memory that contains an instance of that struct. For example consider the two structs A & B:
typedef struct {
int iNum;
float fNum;
char cStr[80];
}A;
A a, *pA; //create an instance, and pointer to an instance of A
typedef struct {
int iNum1;
int iNum2;
int iNum3;
}B;
B b, *pB; //create an instance, and pointer to an instance of B
A & B are obviously different, and will occupy a different size and shape in memory, so if the pointer to B, *pB was set to point to anything but B, it would be incorrect. It needs to point to B
Correct:
pA = &a //set pointer to A equal to the address of A
pB = &b //set pointer to B equal to the address of B
Incorrect:
pA = &b //set pointer to A equal to the address of B
pB = &a //set pointer to B equal to the address of A
(typical compiler error - Operands of = have types pointer to B and pointer to A)
In C/C++, all parameters are passed by value, not by reference. To get the struct pointer in parameter, you need to use pointer to pointer as the parameter. Like this:
int P1_SemCreate(char *name, unsigned int value, P1_Semaphore **sem){
...
*sem = temp;
...
}
HERE IS MY INSTRUCTIONS FOR THIS FUNCTION: Here an unsigned integer listsize is passed to this function you are to create a link list of size listsize. This will be performed by repeated use of malloc and calling setData to initialize the data into the struct plane fields. Each time you place the process in the list you need to place it so the list is sorted by the field distance (in ascending order). you return the head of the list
struct plane* list_intialize(unsigned int num)
{
struct plane *ptr,*head;
int i=0;
ptr = (struct plane*) malloc(num * sizeof(struct plane));
for (i = 0; i < num; ++i)
setData(ptr+i);
return ptr;
}
This started as a function skeleton inside an already completed program....I'm to complete the function so that it creates a link list. The setData is given function that inserts data to the structure elements.....MY problem is that after I run the current function it only returns one plane with information instead of num amount....am I using setData wrong or should my current setup work
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef _MY_DEF_
#define _MY_DEF_
enum dir {NE=0, EN, NW, WN, SE, ES, SW, WS};
enum loc {LNE=0, LNW,LSE,LSW};
struct plane{
short flightCode;
long xCord;
long yCord;
double distance;
char direction;
enum dir flightPattern;
enum loc location;
struct plane *nextPlane;
};
#endif
struct plane* sortByDist(struct plane*);
struct plane * radarPrint(struct plane*head);
int checkPlane(struct plane *);
int checkForCollision(struct plane*);
void setData(struct plane *pLane);
You need to allocate your list by allocating each node. One way of doing that while chaining the list forward is the code below:
struct plane* list_intialize(unsigned int num)
{
struct plane *head, **pp = &head;
int i=0;
for (i=0; i<num; ++i)
{
*pp = malloc(sizeof(**pp));
setData(*pp);
pp = &(*pp)->nextPlane;
}
*pp = NULL;
return head;
}
How It Works
This uses a pointer-to-pointer to always hold the address of the location where the nextPlane dynamo node address is stored. It starts with the address of the head pointer. With each new node, pp is filled the address of that node's nextPlane member. Once finished, it holds the address of the last node's nextPlane pointer, which it sets to NULL. The first node, pointed to by head, is returned. (and yes, this works even if you passed num = 0 for the requested size, in which case you would get back zero nodes: i.e. NULL).
Note: Don't forget, you need to free each node when releasing the list, extracting a single node out, etc. For example, to delete an entire list:
void list_delete(struct plane **lst)
{
while (*lst)
{
struct node *victim = *lst;
*lst = victim->nextPlane;
free(victim);
}
}
Invoked like this:
struct plane *lst = list_initialize(N);
// use list.., maybe adding nodes, removing them, changing, etc...
list_delete(&lst);
How to print your list:
void list_print(const struct plane *lst)
{
while (lst)
{
// TODO: print list node pointed to by lst.
// Ex: (x,y) coords
printf("(%d,%d) ",lst->xCord, lst->yCord);
lst = lst->nextPlane;
}
printf("\n");
}
You are not setting the links between the objects. In the for loop, you need:
ptr[i]->nextPlane = ptr[i+1];
At the end of the loop, make sure the last object points to NULL.
ptr[i-1] = NULL;
I want to pass struct members in function . I don't mean something like that:
struct smth
{
int n;
};
void funct(struct smth s);
I want these structs
struct student {
char name[50];
int semester;
};
struct prof {
char name[50];
char course[50];
};
struct student_or_prof {
int flag;
int size;
int head;
union {
struct student student;
struct prof prof;
}
}exp1;
struct student_or_prof *stack;
struct student_or_prof exp2;
To pass their members in a fucntion with variables not struct variables
int pop(int head,int n)
{
if(head==n)
return 1;
else head++;
}
Because i don't want to use the function for structs only. Is it possible?
EDIT I want the numbers also to change , not return , something like pointer.
EDIT_2 Also i know that this pop(exp1.head,n) it works, but i want also the exp1.head to change after the end of the function pop.
Use pointers. pass poniter to exp1.head and manipulate it by dereferencing it in function as,
int pop(int * head,int n)
{
if(*head==n)
return 1;
else (*head)++;
}
call function as,
pop(&exp1.head,n);
First things first, you are missing a semicolon, after the union definition inside the struct student_or_prof.
As per your edit #2, you should be passing the address of the variable, taking it as a pointer to a variable by the function, and then editing/incrementing the content of the address (the variable that pointer points to). Like the following:
#include <stdio.h>
struct student_or_prof {
int head;
} exp1;
int pop( int * head, int n ) {
if ( *head == n )
return 1;
else (*head)++;
}
int main( ){
int returnval;
exp1.head = 5;
returnval = pop( &exp1.head, 10 );
printf( "%d", exp1.head );
getchar( );
return 0;
}
This will print a 6. Here, I am passing the address of the exp1.head, so that the function pop can refer to the actual exp1.head you have in your hands. Otherwise, the pop will be only informed about the value that exp1.head had, copy that value into its own head variable, play around with that.
And also, it would be sensible to return some int from the pop in any case. Right now it returns a value only when *head == n is satisfied, and returns something that wouldn't make sense. I don't think you'd want that, so:
...
else {
(*head)++;
return 0;
}
...
Would be better.
If you don't like the parenthesis around the *head, then you may want to use ... += 1; instead of a postfix increment, which has less precedence over the dereferencing operator *.