I've searched all over google and this site. I've found a few hints here and there but after i make the changes either my compiler throws a fit or it doesn't produce the desired results.

Here's what i want:
I want to create a linked list in the main function and pass its address to createRandomList so that function can actually fill out the list. Then when thats done i want to pass the list as a copy to the function outputList() this last part seems to be working properly but i'd figure i'd add it in anyways.

Here's what's happening:
When i try to compile this code it generates this error:

test.c: In function `void createRandomList(list**, list**)':
test.c:52: error: request for member `value' in `*randomCurr', which is of non-class type `list*'
test.c:53: error: request for member `next' in `*randomCurr', which is of non-class type `list*'

From what i can gather online this is the appropriate way to pass the pointer to the function for modification. i've also tried the following different lines for line 52. They all generate about the same error.

*randomCurr->value = generateRandom(j,d);
**randomCurr->value = generateRandom(j,d);
**randomCurr.value = generateRandom(j,d);
*randomCurr.value = generateRandom(j,d);
randomCurr.value = generateRandom(j,d);

Here is the code:

struct node{
	int value;
	struct node * next;
typedef struct node list; 

/* Declarations */
void listPrimeExperiment();
int generateRandom(int lo, int hi);
int askQuestion(char *question);
void outputList(list *curr);
void createRandomList(list **randomCurr, list **randomHead);

int main(void) {
	return 0;
void listPrimeExperiment() {
	int l;
	list *listCurr, *listHead;
	int Q = askQuestion("Generate random List type 1, \n Ascending List type 2, \nDecending List type 3");
	if(Q == 1){
		createRandomList( &listCurr, &listHead);
	//If i can get the code to compile this test then generates either garbage or 0.
	printf("%d \n", listCurr->value);  
	/* Print the unsorted list */
	printf("list is as follows \n");
	outputList(listCurr);  // I want this to pass a copy so it won't modify the list
void createRandomList(list **randomCurr, list **randomHead){
	randomHead = NULL;
	int l;
	int n = askQuestion("How big of a list to make");
	int j = askQuestion("starting place for random numbers?");
	int d = askQuestion("ending place for random numbers?");
	for(l=0; l< n; l++){
		*randomCurr = (list*)malloc(sizeof(list));
		*randomCurr->value = generateRandom(j,d);
		randomCurr->next  = randomHead;
		randomHead = randomCurr;
	randomCurr = randomHead; 
void outputList(list *curr){
	int i = 0;
	while(curr) {
		if(i % 20 == 0) printf("\n");
		printf(" %d ", curr->value);
		curr = curr->next ;
6 Years
Discussion Span
Last Post by Martin B

First thing....Your function call "listPrimeExperiment()" where does it save/pass the address of the linkedlist? What I see is two local variables that don't pass back the addresses of the the linked list....So how can you access these unknown values?


That's the problem I'm having I guess. randomCurr should be an alias for listCurr. So when I'm editing randomCurr it should be modifying listCurr that's located in listPrimeExperiment. I guess the code is more messed up than I thought.


You want:

(*randomcur)->value = generateRandom(j,d);


*randomcur->value = generateRandom(j,d);

is the same as

*(randomcur->value) = generateRandom(j,d);
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.