We help IT Professionals succeed at work.

Issues with pointer?

GPicasso
GPicasso asked
on
I just can't kill these two errors no matter how hard I try someone please explain this to me
gcc salist.c test.c -o test
test.c: In function ‘main’:
test.c:15:8: error: storage size of ‘list’ isn’t known
test.c:18:3: error: type of formal parameter 2 is incomplete

#include <stdio.h>
#include <stdlib.h>
//#include "salist.h"

typedef struct SList SList;
typedef int* PtrToArray;
typedef int* Position;
typedef int ElementType;
struct SList{
	PtrToArray storage;
	int size;
};

void Insert(ElementType element, SList list)
{
	int i;
	if (list.storage)
	{
		list.storage = realloc( list.storage, (sizeof(ElementType) * ++list.size) );
		for (i=list.size-1; i>0; i--)
		{
			list.storage[i] = list.storage[i+1];

		}
		list.storage[0] = element;

	}
	else
	{
		list.storage = malloc(sizeof(ElementType));
		list.storage[0] = element;
	}



}
Position Find(ElementType element, SList list)
{
	Position r = 0;
	int i=0;
	while(list.storage[i] != element && i <list.size)
	{
		i++;
	}
	if (i<list.size)
	{
		while( i > 0)
		{
			list.storage[i] = list.storage[--i];
		}
	list.storage[0] = element;
	r = list.storage;
	}
	return r;
}
void Delete(ElementType element, SList list)
{
	int i = 0;
	while (list.storage[i] != element)
	{
		i++;
	}
	while(i<list.size)/*move every remaining element in the array back one*/
	/*reallocate the array to be one smaller than it was*/
	{
		list.storage[i++]=list.storage[i];

	}
	list.storage = realloc( list.storage, (sizeof(ElementType) * --list.size) );

}

void PrintList(SList list)
{
	int i;
	for (i = 0; i<list.size;i++)
	{
		printf("%d\n",list.storage[i]);
	}
}

Open in new window

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

int main()
{
	int i;
	SList list;
	for (i = 0; i < 28; i++)
	{
		Insert(i, list);
	}

return 0;
}

Open in new window

Comment
Watch Question

Top Expert 2009

Commented:
Could you post your salist.h file here ?

Author

Commented:
Well that's another thing.  I wrote up the salist.h and it kept telling me that I had duplicate definitions,  I'm really not too clear on why.  Maybe someone could explain this to me as well.
#ifndef SALIST_H_
#define SALIST_H_
struct SList;
typedef struct SList SList;
typedef int *PtrToArray;
typedef int *Position;
typedef int ElementType;
void PrintList( SList list);
void Insert( ElementType element, SList list);
void Delete( ElementType element);
Position Find(ElementType element, SList list);

#endif /* SALIST_H_ */

Open in new window

Top Expert 2009

Commented:
Ok,

You need to define the SList struct in the header file, and then include that header file were you need to use it.

So, salist.h will contain :
#ifndef SALIST_H_
#define SALIST_H_

typedef int *PtrToArray;
typedef int *Position;
typedef int ElementType;

struct SList{
	PtrToArray storage;
	int size;
};

typedef struct SList SList;

void PrintList( SList list);
void Insert( ElementType element, SList list);
void Delete( ElementType element);
Position Find(ElementType element, SList list);

#endif /* SALIST_H_ */

Open in new window


and the salist.c file will start by including salist.h :
#include "salist.h"

/* other includes go here */

/* the function definitions (implementations) go here */

Open in new window


and you also include salist.h in your main.c like you already did.

Author

Commented:
Now I get these errors which went away when i commented out the include, it seems it's one or the other

salist.c:5:22: error: redefinition of typedef ‘SList’
salist.h:20:22: note: previous declaration of ‘SList’ was here
salist.c:6:14: error: redefinition of typedef ‘PtrToArray’
salist.h:11:14: note: previous declaration of ‘PtrToArray’ was here
salist.c:7:14: error: redefinition of typedef ‘Position’
salist.h:12:14: note: previous declaration of ‘Position’ was here
salist.c:8:13: error: redefinition of typedef ‘ElementType’
salist.h:13:13: note: previous declaration of ‘ElementType’ was here
salist.c:9:8: error: redefinition of ‘struct SList’
salist.h:15:8: note: originally defined here
salist.c:56:6: error: conflicting types for ‘Delete’
salist.h:24:6: note: previous declaration of ‘Delete’ was here
test.c: In function ‘main’:
test.c:15:2: error: ‘SList’ undeclared (first use in this function)
test.c:15:2: note: each undeclared identifier is reported only once for each function it appears in
test.c:15:8: error: expected ‘;’ before ‘list’
test.c:18:13: error: ‘list’ undeclared (first use in this function)
Top Expert 2009
Commented:
>> salist.c:5:22: error: redefinition of typedef ‘SList’

That's because you left the typedef's and struct definition in the salist.c file.

Take a closer look at the salist.c template from my previous post, and notice that I only left place holders for the includes and the function definitions - not the typedef's and struct definition (they belong in the salist.h file after all).

Author

Commented:
Ok I comented out the typedefs from salist.c and also fixed a mistake in my delete signature.
I still get the following errors  I suspect the first two can be fixed easy enough.  But the ones in main are pretty confusing..  it seems like such a simple program!

gcc salist.c test.c -o test
salist.c:10:8: error: redefinition of ‘struct SList’
salist.h:15:8: note: originally defined here
test.c: In function ‘main’:
test.c:15:2: error: ‘SList’ undeclared (first use in this function)
test.c:15:2: note: each undeclared identifier is reported only once for each function it appears in
test.c:15:8: error: expected ‘;’ before ‘list’
test.c:18:13: error: ‘list’ undeclared (first use in this function)

Author

Commented:
AH i see just as you said.. thank you
Top Expert 2009

Commented:
Glad to be of assistance :)