I am almost certain that I got this wrong, but I'm not sure how it would be right. It currently doesn't compile, but it is more the concept I am looking for. Pointers are the only things really confusing me in C right now (Although I'm sure other things will come). Anyways, I am just trying to do the first couple of functions for Linked Lists from my book. It would be great if someone could give some small code (and explain it), because that seems to be the way that I learn this sort of thing.
Code:
Sorry for the bad code :/
Edit: Rather than giving random code that is probably hard to follow, I'll just ask some general questions: If I want to pass a pointer to a routine and be able to edit the pointer as well as the data at that pointer, how would I do it? If I wanted to pass a variable to a function and have it be edited and the edit remain after the function, would I do it similar to this:
Code:
How would I just pass an array and edit the values in a function?
Code:
#include <stdio.h>
typedef struct NODE {
int data;
NODE* next;
};
// Function prototypes:
void DisplayList(NODE* head);
void Insert(NODE* head, int value);
void main() {
NODE* head = 0;
Insert(head, 10);
Insert(head, 20);
Insert(head, 30);
DisplayList(head);
}
void DisplayList( NODE* head ) {
NODE* node;
for(node = head; node; node = node->next) {
printf("%d\n",node->data);
}
}
void Insert(NODE* head, int value) {
NODE* newnode;
newnode = malloc(sizeof(struct NODE));
if( !newnode )
return 0;
newnode->data = value;
newnode->next = head;
head = newnode;
return 1;
}
Edit: Rather than giving random code that is probably hard to follow, I'll just ask some general questions: If I want to pass a pointer to a routine and be able to edit the pointer as well as the data at that pointer, how would I do it? If I wanted to pass a variable to a function and have it be edited and the edit remain after the function, would I do it similar to this:
Code:
void main() {
int a = 5;
MultByFive( &a );
}
void MultByFive( *value ) {
*value = *value * 5;
}