PDA

View Full Version : programming help (typedef, c, list, pointers, syntaxt probems likely)



cirthix
02-12-2007, 12:08 AM
Ok, so I thought about how to implement a list for a while and came up with something:

if kept updated, doubly linked, indexed list can be treated *almost* as an array, if the functions are put together properly. however, I'm getting a whole ton of errors when I try to compile it. I know I'm really bending some rules with this one, but is there a way to force-ignore mismatching data types and stuff?

a quick explanation of the code:

insert takes the data, list, and place and makes a new cell, moves the pointer sof the cells that are supposed to be next to it, then updates the list's numbering
rewrite overwrites a cell's data with new data
remove switches the next and previous pointers around near the cell to be removed, then renumbers the list, then frees the memory
addr is really what makes this list implementation act like an array. it allows psuedo-random(though just obscured searching) access. it goes thought he list and returns the address of the proper cell

almost all of the manipulation of the elements is done through ponters. the logic should be correct. I'm pretty sure that the majority of the errors are coming from something to do with the typedefs, but i can't pinpoint what.

there's a simple driver that just prints a zero. once the list compiles properly, it can easily be edited to print the entered number. it's the simplest driver i could think of.

now that you know what it's *supposed* to do, here's the code:
typedef struct {int number;} data;
typedef struct {data stuff; int count; int* next; int* prev;} ent;
typedef struct {int entries; ent* top; ent* bottom;} list;

list* make();
void insert(list* a, data stuff, int position);
void rewrite(data stuff, int position);
void remove(int position, list* a);
ent* addr(list* a, position);


int main ()
{
int y;
list* x=make();
printf("enter a number");
scanf("%d", &y);
add(y, &x);
printf("%d\n", 0;
}

list* make()
{
return calloc(sizeof(list));
}


void insert(list* a, data stuff, int position)
{
ent* tmp, y, z;
int x=0;

if (position<=a.count+1 && position>0)
{
tmp=malloc(sizeof(ent));
y=addr(position);
z=*(y+sizeof(int)+sizeof(tmp));
*(tmp)=position;
*(tmp+sizeof(int)+2*(sizeof(tmp)))=stuff;
a.count++;
if(position>1)
{
*(z+sizeof(int))=tmp;
*(tmp+sizeof(int)+sizeof(tmp))=z;
}
else{a.back=tmp;}

if(position<a.count)
{
*(y+sizeof(int)+sizeof(tmp))=tmp;
*(tmp+sizeof(int))=y;
while (position<=a.count)
{
*(y)++;
y=*(y+sizeof(int));
position++;
}
else{a.back=tmp;}
}
else {printf("error\n");}
}



void rewrite(data stuff, int position)
{
int* useless;
*(addr(position)+sizeof(int)+2(sizeof(useless)))=s tuff;
}

void remove(int position, list* a)
{
ent* tmp=addr(position);
ent* tmp2=tmp;
if (position!=1)
{*(*(tmp+sizeof(int)+sizeof(tmp))+sizeof(int))=*(t mp+sizeof(int));}
else{a.back=*(a.back+sizeof (int));}
if (position!=a.count)
{*(*(tmp+sizeof(int))+sizeof(tmp)+sizeof(int))=*(t mp+sizeof(int)+sizeof(tmp));}
else{a.front=*(a.front+sizeof(int)+sizeof(tmp));}
a.count--;
while(position<a.count)
{
*tmp--;
tmp=*(tmp+sizeof(int));
position++;
}
*tmp--;
}

ent* addr(list* a, int position)
{
ent* tmp=a.back;
int x=0;
while (x<position)
{
tmp=*(tmp+sizeof(int));
x++;
}
return tmp;
}


cc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment


gcc errors:
list2.c:9: error: parse error before "position"
list2.c: In function `main':
list2.c:19: error: parse error before ';' token
list2.c: In function `insert':
list2.c:33: error: request for member `count' in something not a structure or union
list2.c:35: warning: assignment makes pointer from integer without a cast
list2.c:36: error: incompatible types in assignment
list2.c:37: error: invalid operands to binary +
list2.c:38: error: incompatible types in assignment
list2.c:39: error: incompatible types in assignment
list2.c:40: error: request for member `count' in something not a structure or union
list2.c:43: error: invalid operands to binary +
list2.c:46: error: request for member `back' in something not a structure or union
list2.c:48: error: request for member `count' in something not a structure or union
list2.c:50: error: invalid operands to binary +
list2.c:52: error: request for member `count' in something not a structure or union
list2.c:54: error: wrong type argument to increment
list2.c:55: error: invalid operands to binary +
list2.c:58: error: parse error before "else"
list2.c: In function `rewrite':
list2.c:68: error: called object is not a function
list2.c: In function `remove':
list2.c:76: error: invalid operands to binary +
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:77: error: request for member `back' in something not a structure or union
list2.c:78: error: request for member `count' in something not a structure or union
list2.c:79: error: invalid operands to binary +
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:80: error: request for member `front' in something not a structure or union
list2.c:81: error: request for member `count' in something not a structure or union
list2.c:82: error: request for member `count' in something not a structure or union
list2.c:85: error: incompatible types in assignment
list2.c: In function `addr':
list2.c:93: error: request for member `back' in something not a structure or union
list2.c:97: error: incompatible types in assignment

ahmad
02-12-2007, 04:21 AM
printf("%d\n", 0;

is missing )

Then you have a lot of errors where you did x.count or x.back to get a property but that won't work if x is a pointer to that element. To follow the pointer and access the property you have to do x->count or x->back.

Fix those bugs, then recompile. It will be easier to find the rest when those are gone.

cirthix
02-13-2007, 11:43 PM
compiles, but segfaults
#include <stdio.h>
#include <stdlib.h>


typedef struct {int number;} data;
struct ent_t {data stuff; int count; struct ent_t* next; struct ent_t* prev;};
typedef struct ent_t ent;
typedef struct {int num; ent* top; ent* bot;} list;

list* make(void);
void insert(list* a, data stuff, int position);
void rewrite(list* a, data stuff, int position);
void rem(int position, list* a);
ent* addr(list* a, int position);


int main ()
{
data y;
list* x;
x=make();

printf("enter a number \n");
scanf("%d", &y.number);
insert(x, y, 1);
printf("%d \n", y.number);
return 0;
}

list* make(void) {list* a; a=calloc(1, sizeof(list)); a->top=NULL; a->bot=NULL; return a;}


void insert(list* a, data stuff, int position)
{
ent* tmp;
ent* n;
ent* p;

if (position<=a->num+1 && position>0)
{
tmp=malloc(sizeof(ent));

n=addr(a, position);
p=n->prev;
tmp->count=position;
tmp->stuff=stuff;
a->num++;
if(position>1) {p->next=tmp; tmp->prev=p;}
else{a->bot=tmp;}

if(position<=a->num)
{
n->prev=tmp;
tmp->next=n;
while (position<=a->num) {n->count++; n=n->next; position++;}
}
else{a->top=tmp;}
}

else {printf("error\n");}
}



void rewrite(list* a, data stuff, int position)
{
ent* u;
u=addr(a, position);
u->stuff=stuff;
}

void rem(int position, list* a)
{
ent* n;
ent* p;
ent* c;
c=addr(a, position);

if (position!=1) {p=c->prev; p->next=c->next;}
else{a->bot=c->next;}

if (position!=a->num) {n=c->next; n->prev=c->prev;}
else{a->top=c->prev;}
a->num--;
while(position<=a->num)
{
n->count--;
n=n->next;
position++;
}
free(c);
}

ent* addr(list* a, int position)
{
ent* tmp;
int x=0;
tmp=a->bot;
while (x<= position)
{
tmp=tmp->next;
x++;
}
return tmp;
}

ahmad
02-14-2007, 09:23 AM
You have a major flaw in your logic and that is NULL values are not being accounted for. In your addr function, a.bot is null to begin with and you are working with it. Just do some error checking if the value is NULL. But this would be an easy fix and wouldn't solve your problem. Later on in your code, you attempt to use the value returned by the addr function to retrieve the previous.

You can avoid all of these problems by initializing top and bottom to non-NULL values, but have them contain no data and have them contain NULL prev and next. This is sort of moving the nulls down the tree, but it will save you reworking your code.

If that is not desirable, then you have to introduce NULL error checks and handling.