As you can see this presents somewhat of a problem for me, I can't put either one first so what do I do?
arowland
10-09-2000, 07:29 PM
Let's see. Reducing the logic to math sometimes helps. We have
a * b = t
and
t * c = a
so
(t * c ) * b = t which reduces to c * b = 1
or
(a * b) * c = a which reduces to b * c = 1
The only deduction is that both b and c must be positive or negative and that one is <(-)1 and the other >(-)1 or both are (-)1.
What are you trying to do here?
If you'r just concatinating strings, where's the problem?
[This message has been edited by arowland (edited 09 October 2000).]
Keith M
10-09-2000, 10:07 PM
I can't remember the proper syntax, but the keyword you want is "forward". Look that up and you should be set.
pdc
10-10-2000, 02:23 AM
Dunno,
Go purchase THE K&R C book, whatever is the latest edition. It is the Bible and will help you solve many of the problems you are having.
Paul
Strike
10-10-2000, 03:28 AM
Originally posted by dunno:
Here is a problem I have:
typedef struct {
apple * a;
} tree;
typedef struct {
tree * b;
} apple;
As you can see this presents somewhat of a problem for me, I can't put either one first so what do I do?
This, to me, looks like serious bad news and there's a reason it shouldn't work. Let's say that somehow you do get this code to work. Okay, so you declare a variable:
apple GrannySmith;
Well, GrannySmith has a member of type tree named b. And b, being of type tree has a member of type apple named a. And a being of type apple has a member of type tree named b. And so on, and so forth. The recursion never ends. This should never compile because the compiler couldn't possibly reduce this down to a finite set of memory allocations.
Keith M
10-10-2000, 09:59 AM
Strike, that same logic would prevent compilers from using linked-lists and the like. While his example doesn't make a lot of sense, its certainly a valid thing to do. I believe you want to do something like this:
forward apple;
typedef struct {
apple * a;
} tree;
typedef struct {
tree *b;
} apple;
Strike
10-10-2000, 10:24 AM
Originally posted by Keith M:
Strike, that same logic would prevent compilers from using linked-lists and the like. While his example doesn't make a lot of sense, its certainly a valid thing to do. I believe you want to do something like this:
forward apple;
typedef struct {
apple * a;
} tree;
typedef struct {
tree *b;
} apple;
Ah, so true. Thanks for correcting me. I guess I didn't think of linked lists because those just involved structs (nodes) pointing to other ones of the same type, and not this back and forth situation. Of course, this begs the question ... how does the compiler handle this? Oh, wait ... duh, it's probably since we're dealing with pointers that this is fine, because until you allocate memory for a pointer, the members don't need memory either. So, that stuff is either known at compile time or is addressed at runtime ... excuse me while I go wipe the egg off my face http://www.linuxnewbie.org/ubb/smile.gif
dunno
10-10-2000, 04:43 PM
That forward thing doesn't seem to work.
You see this is just an example, but here's why it's needed:
I have a person struct, a person has certain features, such as eyecolor and height. Also noted is where this person is located at the time.
The location the person is at is of type place, place has some features, such as what objects are there and how many people are there. (person persons[100] http://www.linuxnewbie.org/ubb/wink.gif.
And the book idea won't work, because I have proven to myself that I can learn no programming from books, because I have a C++ book, a Java book, and a VB book, and another C++ book and a VC++ book. Which languages do I know you ask? I can code the basics of C++ (nothing past basic classes), no java, enough VB to do a few things, no VC++. The only thing I consider myself good at is C, and I have no book for it.
And I gots no $$$
[This message has been edited by dunno (edited 10 October 2000).]
TheLinuxDuck
10-10-2000, 05:39 PM
Now, here's a thought for you. It seems to me that putting a pointer to the struct 'person' in the location struct is a little more than you need.
Each person is going to have a specific number, so why not just have an integer that represents the person inside the location struct?
#include<stdio.h>
#include<string.h>
struct person {
char eyecolor,haircolor,height,weight;
short int location;
char name[40];
};
struct location {
short int people[100];
char name[40];
};
// Then just use the integer to display the
//correct person...
int main(void)
{
struct person people[100];
struct location area[10];
strcpy(people[0].name,"Jeffrey Jailhouse");
people[0].location=0;
strcpy(area[0].name,"The Pub");
area[people[0].location].people[0]=people[0].location;
// Set to our first person, Jeffrey jailhouse
printf("%s is in location %s.\n",
people[area[people[0].location].people[0]].name,
area[people[0].location].nam
e);
return 0;
}
This seems much simpler than dealing with pointers to pointers to pointers.. http://www.linuxnewbie.org/ubb/smile.gif This way, you're only using 2 bytes per person at a specific location.
Does this make sense, or seem like it will work for you?
------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq
[This message has been edited by TheLinuxDuck (edited 10 October 2000).]
[This message has been edited by TheLinuxDuck (edited 10 October 2000).]
dunno
10-10-2000, 05:50 PM
What you are suggesting wouldn't work unless I had a predetermined number of people, and I don't.
Basically the number of people will change during the course of the program.
As it is now, each thread creates a person and a place pointer. Each function that uses these values is sent a person and the place pointer, it would be much better for me, if I could simply send a pointer to a person, which would include his location.
[This message has been edited by dunno (edited 10 October 2000).]
Stuka
10-10-2000, 06:58 PM
Does your location need to know WHO is there, or merely HOW MANY people are there? If it's just how many, couldn't you have a counter in each location struct that got incremented/decremented every time someone entered/left? Seems like that would be easier.
dunno
10-10-2000, 07:11 PM
Each location needs to know who is there, it will contain all of their data.
[This message has been edited by miller (edited 10 October 2000).]
dunno
10-10-2000, 08:58 PM
Well, at least it compiles, so far it seems like it may work, thanks.
TheLinuxDuck
10-10-2000, 10:06 PM
I don't know exactly what you're after, but it looks like you're wanting a linked list (dynamic list of people and areas).
I think something like:
#include <stdio.h>
struct person {
char age,weight,eyecolor;
struct person *prevperson;
struct person *nextperson;
struct location *currentlocation;
char name[40];
short int uniqueID;
};
struct location {
char name[40];
struct location *prevlocation;
struct location *nextlocation;
short int uniqueID;
};
This would allow you to dynamically add to people and locations as you needed. This would also allow you to have as many people in one location as you wanted. The only trouble with it is, you couldn't look at the location and determine how many people are there at any given time, unless you build a ID array that contains the uniqueID of each person. like:
struct location {
char name[40];
struct location *prevlocation;
struct location *nextlocation;
short int uniqueID;
short int people[500];
};
This way it is giving a limit of 500 people, taking up 1000 bytes per area. You could probably reduce it to 250 or even 200 and get away with it. If you're trying to model it after a real area, you can't have a million people in one place anyway. Maybe the amount could be dynamic such as:
struct location {
char name[40];
struct location *prevlocation;
struct location *nextlocation;
short int uniqueID;
short int *people;
short int totalPeople;
};
So, when you dynamically allocate the memory for the area, you could also define a specific number of people allowed, depending on what the area is, and allocate the room in short int *people. (Like a small pub would only allow 125, whereas a courtyard could hold 300). And since you've got the totalpeople, when a person tried to move into that area, you could make sure that there was enough room, and tell them that it is too crowded.
But, in your code, you'd have to remember to set not only the pointer to the location of the person, but also add his/her unique ID into the *people.
But, it would give you not only a way to tell, by person, where they are at, it also gives you the ability to list, per area, who is in the area..
dunno, if you can't tell, this is pretty exciting for me.. http://www.linuxnewbie.org/ubb/wink.gif I love figuring out stuff like this.. I may not always come up with the best way, but I sure have fun thinking it through and trying to create a solution.
I am curious to know how you decide to do this. http://www.linuxnewbie.org/ubb/smile.gif if you don't mind, you'll have to keep us (me) posted.. http://www.linuxnewbie.org/ubb/wink.gif
------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq
pdc
10-11-2000, 01:18 AM
Dunno,
Trust me, get the K&R book. The rules that the compilers are written to are defined, pretty much, by this book.
And now, for something completely different. You are coding a problem that would be better handled by a relational database. Pretty easy, also.
But you can do it in C. here is what you can do. Create a linked list of locations. I use a pointer to a pointer array with three elements: head, current and last. The people have a relationship with the locations.
You can cast people to be whatever you decide a people to be or make it a pointer to a people struct in the location struct. I use some defines, like:
#define lochead lptr[0]
#define loccurrent lptr[1]
#define loctail lptr[2]
and
struct location *lptr[3];
as there is a single chain of locations and multiple chains of people you will probably need to dynamically allocate the people chain pointer arrays, as needed.
Paul
justlinux.com
Copyright Internet.com Inc. All Rights Reserved.