Is it possible to modify a link list created in the main function in another function without using a return. I'm asking because in my assignment I want to modify 3 link lists in the function and as far as I know it's only possible to return one modified link list. I figured out how to modify a simple int variable that way and a char array, but can't seem to figure out the proper syntax for the link lists.
For the int variable:
Code:
#include <stdio.h>
void changeVar(int *x);
int main(void){
int x=0;
changeVar(&x);
printf("x = %d\n\n", x);
system("PAUSE");
return 0;
}
void changeVar(int *x){
*x=2;
}
For the array:
Code:
void changeArray(char *arr);
int main(void){
char array[10];
array[0]='d';
changeArray(array);
printf("array[0] = %c\n\n", array[0]);
system("PAUSE");
return 0;
}
void changeArray(char *arr){
arr[0]='a';
}
Here is what I have for the link list (I've tried different *, **, and & configs) :
Code:
// Link List Struct
typedef struct KnightsMartRestockProduct {
int itemNum;
struct KnightsMartRestockProduct *next;
} KMRestockProduct;
// Function Header
void customer(KMRestockProduct *restockList);
int main(void){
KMRestockProduct* KMRestockList=(KMRestockProduct *)malloc(sizeof(KMRestockProduct));
customer(KMRestockList); // Call to function
printf("itemNum = %d\n\n", KMRestockList->itemNum);
system("PAUSE");
return 0;
}
void customer(KMRestockProduct *restockList){
KMRestockProduct* help_ptr=restockList;
KMRestockProduct* pNewRestock=(KMRestockProduct *)malloc(sizeof(KMRestockProduct));
pNewRestock->itemNum = 2;
pNewRestock->next = NULL;
if(restockList==NULL){
pNewRestock->next=restockList;
restockList=pNewRestock;
}
while(help_ptr->next != NULL && help_ptr->next->itemNum < pNewRestock->itemNum)
help_ptr=help_ptr->next;
pNewRestock->next=help_ptr->next;
help_ptr->next=pNewRestock;
}
I'm just trying to modify the itemNum field of the link list from inside the function without having to use return.