Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

I'm a beginner with headers

header c c++ include

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 abderrahim

abderrahim

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts

Posted 30 December 2012 - 01:03 PM

Hi everybody!!
I hope i'm correct in my assumptions, please correct me when I get it wrong,
I'm using simple functions manipulating lists, in another function; so here is my code:


}
#include <stdlib.h>
#include "lists.h"

typedef struct element element;
struct element{
int val;
element *next;
};
typedef element* list;
list createList(){
list l = NULL;
return l;
}
list addElement(int a,list l){
element* nod = malloc(sizeof(element));
nod->val = a;
nod->next = l;
return nod;
}
void showList(list l){
element* temp=l;
if (l==NULL){
printf("la list est vide");
}else{
while(temp->next!=NULL){
	 printf("nodd: %d",temp->val);
	 temp=temp->next;
}
printf("nodd: %d",temp->val);
}
}
#include <stdlib.h>
#include "lists.h"

int main(int argc, char *argv[]){
int i;int length=0; int bool=0; int j;
list l=createList();
l=addElement(l,2);
element* temp=l;
for(i=3;i<atoi(argv[1]);i++){
while((temp->next)!=NULL){
//printf("nodd: %d",temp->val);
temp=temp->next;
if(i % temp->val==0){bool=1;break;}
}
if(bool==0){
temp=temp->next;
temp->val=i;
}
bool=0;
}
showList(l);
return 0;
}

}

Im compiling with tthis command:
Compile command: gcc -o lists primeGen lists.c primeGen.c

here are errors generated:
primeGen.c:10:16: error: dereferencing pointer to incomplete type
primeGen.c:12:14: error: dereferencing pointer to incomplete type
primeGen.c:13:16: error: dereferencing pointer to incomplete type
primeGen.c:16:14: error: dereferencing pointer to incomplete type
primeGen.c:17:9: error: dereferencing pointer to incomplete type

Thank you in advence

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 30 December 2012 - 08:34 PM

There are few errors --
  • The list type and member functions (createList, addElement, showList) should be declare in the lists.h header file
  • The return value of malloc should be type cast-ted to the list type because it returns void* type.
  • You are passing wrong argument to addElement call in main.c file. Actually the argument order's are reversed.
You need to use an IDE to fix all those errors quickly.

Following is the fixed code --
// ****** lists.h file content ******

typedef struct element element;
struct element{
   int val;
   element *next;
};
typedef element* list;

list createList();
list addElement(int a,list l);
void showList(list l);

// ****** lists.c file content ******

#include <stdlib.h>
#include <stdio.h>
#include "lists.h"


list createList(){
list l = NULL;
return l;
}

list addElement(int a,list l){
element* nod = (element*) malloc(sizeof(element));
nod->val = a;
nod->next = l;
return nod;
}


void showList(list l){
element* temp=l;
if (l==NULL){
printf("la list est vide");
}else{
while(temp->next!=NULL){
         printf("nodd: %d",temp->val);
         temp=temp->next;
}
printf("nodd: %d",temp->val);
}
}

// ****** main.c file content ******

#include <stdlib.h>
#include "lists.h"

int main(int argc, char *argv[]){
int i;int length=0; int boolean=0; int j;
element* temp;
list l=createList();
l=addElement(2, l);
temp=l;
for(i=3;i<atoi(argv[1]);i++){
while((temp->next)!=NULL){
//printf("nodd: %d",temp->val);
temp=temp->next;
if(i % temp->val==0){boolean=1;break;}
}
if(boolean==0){
temp=temp->next;
temp->val=i;
}
boolean=0;
}
showList(l);
return 0;
}


#3 abderrahim

abderrahim

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 58 posts

Posted 03 January 2013 - 01:47 AM

OK, I think I got what you said, thank you so much kernelcoder




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download