• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

programming help (typedef, c, list, pointers, syntaxt probems likely)

cirthix

Diamond Member
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
 
Use code tags.
- EDIT -
OK, so don't use code tags, they don't seem to work
- END EDIT -

Comment your code.
If you'd indented your code propery, you'd see that your braces don't match.
Name your variables something sensible.
READ the compiler errors - "list2.c:9: error: parse error before "position"" likely means that you should find the word "position" on line nine, and think about what's wrong with it (does it need a datatype?).

Aside from that, heres a few selected errors:

You're missing includes (for printf and scanf)

Code:
ent* addr(list* a, position);
position needs a datatype... (int)

Code:
add(y, &x);
You don't have a function called "add".

Code:
printf("%d\n", 0;
misses a )

Code:
return calloc(sizeof(list));
calloc takes 2 arguments

Code:
if (position<=a.entries+1 && position>0)
"list" doesn't have any member called "count". Nor does it have any "back" You mean "entries" (which isn't initialized). Members in pointers to structs are accessed with ->.

Code:
ent* tmp, y, z;
tmp is a pointer, y and z are not. You wan't all of them to be pointers.

Code:
z=*(y+sizeof(int)+sizeof(tmp));
I'm not sure what you're trying to do, but i don't think this is it.

Code:
*(tmp)=position;
I'm not sure what you're trying to do, but i don't think this is it.

Code:
*(tmp)=(list*)position;
I'm not sure what you're trying to do, but i don't think this is it.

Code:
y=addr(position);
You've defined addr to take two parameters.
 
This line:
ent* addr(list* a, position);

Is not legal. You did not specify a type decleration for position.
 
Neverm1nd basically told you everything that was wrong but in general, don't be intimidated by a big long list of compile errors. Just go through them, 1 by 1 and figure out what's wrong. Use those line numbers it gives you and hunt for that missing semicolon, unbalanced parentheses, or omitted data type.

I have to admit, I am a bit surprised that you are dealing with pointers, addresses, pass by reference, and implementing linked list functionality when you are having problems seeing that brackets are unbalanced and data types are missing. Everybody makes those simple errors but you need to be able to recognize those easy problems quickly. I don't mean to be discouraging, but I think you are a bit over your head here. If you are learning on your own, slow down, take a couple steps back and forget about pointers for the time being. Also, while you are coding, think about every character on every line. They are all important.
 
Many of those compiler errors are cascading; one initial error can generate multiple problems down the road.
 
ah, something must not have copied properly. I had those filled in when i went to go check them. weird. I fixed a few of the minor errors, so at this point, I just need to mess iwth the casting and declaring the structs (i don't thik this is done properly). here's new 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(list* a, data stuff, int position);
void remove(int position, list* a);
ent* addr(list* a, int position);


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

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>1)
{
*(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(list* a, data stuff, int position)
{
int* u;
*(addr(a, position)+sizeof(int)+sizeof(u)+sizeof(u))=stuff;
}

void remove(int position, list* a)
{
ent* tmp=addr(a, position);
ent* tmp2=tmp;
if (position!=1)
{*(*(tmp+sizeof(int)+sizeof(tmp))+sizeof(int))=*(tmp+sizeof(int));}
else{a.back=*(a.back+sizeof (int));}
if (position!=a.count)
{*(*(tmp+sizeof(int))+sizeof(tmp)+sizeof(int))=*(tmp+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;
}


and the errors gcc gives:

l.c: In function `make':
l.c:24: warning: return makes pointer from integer without a cast
l.c: In function `insert':
l.c:33: error: request for member `count' in something not a structure or union
l.c:35: warning: assignment makes pointer from integer without a cast
l.c:36: warning: passing arg 1 of `addr' makes pointer from integer without a cast
l.c:36: error: too few arguments to function `addr'
l.c:36: error: incompatible types in assignment
l.c:37: error: invalid operands to binary +
l.c:38: error: incompatible types in assignment
l.c:39: error: incompatible types in assignment
l.c:40: error: request for member `count' in something not a structure or union
l.c:43: error: invalid operands to binary +
l.c:46: error: request for member `back' in something not a structure or union
l.c:50: error: invalid operands to binary +
l.c:52: error: request for member `count' in something not a structure or union
l.c:54: error: wrong type argument to increment
l.c:55: error: invalid operands to binary +
l.c:58: error: parse error before "else"
l.c: In function `rewrite':
l.c:68: error: incompatible types in assignment
l.c: In function `remove':
l.c:76: error: invalid operands to binary +
l.c:77: error: request for member `back' in something not a structure or union
l.c:77: error: request for member `back' in something not a structure or union
l.c:78: error: request for member `count' in something not a structure or union
l.c:79: error: invalid operands to binary +
l.c:80: error: request for member `front' in something not a structure or union
l.c:80: error: request for member `front' in something not a structure or union
l.c:81: error: request for member `count' in something not a structure or union
l.c:82: error: request for member `count' in something not a structure or union
l.c:85: error: incompatible types in assignment
l.c: In function `addr':
l.c:93: error: request for member `back' in something not a structure or union
l.c:97: error: incompatible types in assignment
 
We are not just going to give you the answer to every one of your compile errors. Did you look through them yourself? If so, what did you attempt and what do you think is wrong?

For instance your insert() function takes in a list* a and then tries to access a.count. Do you know the difference between value types and reference types? What does the 'dot' operator do? What does the '->' operator do? Do you know how to access member fields in a struct or class when you only have a pointer to that struct/class (hint: it's not the '.' operator)? If you allocate memory for a structure using sizeof and that structure contains pointers, is there memory automatically allocated for those pointers? (hint: no). If you don't know what I'm talking about, you need to learn. Try Googling "C++ Pointer Tutorial"

I mentioned it before but you seem to be in way over your head here and it is very unlikely that you will be able to get over your learning curve through this thread. Solving all of your basic compile problems is not something we want to do. If you do your own research as to why those errors are occuring and come back with specific questions, there might be more help offered. As it stands, there is way way way too much overcome for this program.
 
significant progress. some namings got changed and mixed me up.

gcc l.c
l.c: In function `make':
l.c:23: warning: assignment makes pointer from integer without a cast
l.c: In function `insert':
l.c:33: warning: assignment makes pointer from integer without a cast
l.c:34: error: incompatible types in assignment
l.c:35: error: invalid operands to binary +
l.c:36: error: incompatible types in assignment
l.c:37: error: incompatible types in assignment
l.c:41: error: invalid operands to binary +
l.c:48: error: invalid operands to binary +
l.c:52: error: wrong type argument to increment
l.c:53: error: invalid operands to binary +
l.c:56: error: parse error before "else"
l.c: In function `rewrite':
l.c:66: error: incompatible types in assignment
l.c: In function `remove':
l.c:74: error: invalid operands to binary +
l.c:75: error: incompatible types in assignment
l.c:77: error: invalid operands to binary +
l.c:78: error: incompatible types in assignment
l.c:83: error: incompatible types in assignment
l.c: In function `addr':
l.c:95: error: incompatible types in assignment
 
Post your new code - WITH comments about what each block of code and function is supposed to do, and please rename your variables to something sensible. If we're going to wade through your code, it will be a lot easier if we don't have to guess everything.
 
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;
}
 
if (position<=a->num+1 && position>0)
num isn't initialized, you should set it to zero. The next error occurs in addr, tmp=a->bot; sets tmp to NULL, since thats the initial value of bot, so tmp=tmp->next; will crash. You also seem to be mixing zero and one-indices, you're not allways setting a new node's prev and next pointers (in insert, tmp->prev and tmp->next should always be assigned to p & n, and you need to find a way to null them if you're inserting at the top or bottom of the list, as well as assigning them to top or bottom when inserting at the head or tail, and you need to check each time a prev or next is read to make sure they're not NULL), the count member in ent doesn't seem to fill any purpouse, you have several functions you're not using (implement them one at a time and make sure each works before going to the next), you still don't have a single comment, your indentation style is weird to say the least, and your variables doesn't have meaningfull names. Here's something you can start to work on (i fixed a few of the errors for you):
 
Back
Top