Any C programmers in the building?

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
Anyway, scanf() will happily read a "string" in to your buffer for you to then strcmp. Or you may want to read 1 char at a time to ensure you don't buffer overrun. As you can't do a switch on a char* you may prefer to use atoi() to convert the "123" to an integer 123.

There are overrun safe string functions in C, have been for a long time. Learn them, love them, dont use anything else!
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
@Sheep/Chilly: Aye, I found that out the fun way.. Going to learn to use the strcmp ways today. I basically need a set of statments (e.g.)

if(responce =="arrive");
if(responce == "leave");

etc -_- but noooooooo, C decided not to use strings, so it never behaved. Got a linear linked list lecture today, whoop.
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
C is a very old language, don't expect it to have the bloat you've grown used to. Also, C is not C++. Don't be tricked by one of these "look at my objects" people!
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
Sheep, Chilly: Am seriously stuck =< Any chance you could help?

Okay, we've (more than 1 of us..) got link lists working with ints, but we have NO idea how to make them working with strings. (We need alpha-numeric enteries.. Entered by the user -_-)

Any example code you could show, or any directions? Pretty much spent since I last posted here looking for code help, but it's failing miserably.

Cheers,

O.D.
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
Ok so a string is really an array of characters. An int is just an int.

A relatively easy way to change from a linked list of ints to a linked list of strings would be to make your linked list a list of pointers to chars (which would be pointing to the 1st character in your array)

So you're structure would change from

Code:
struct Node {
  int value;
  struct Node *next;
};

to

Code:
struct Node {
  char *value;
  struct Node *next;
};

Then you'd iterate as normal, and use something like this to print out the list:

Code:
printf("Value: %s\n", node->value);

This was a bit rushed so it might not be totally right, and it's probably not the best way to do it, but it should be one of the simplest to understand.

The gist of it is that you're not storing your value in the struct anymore, you're storing a pointer to where the start of the string is
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
Your problem is that you store the data directly in the struct. What you should be doing is using a type-agnostic struct to hold the data. Casting and dereferencing is the name of the game.

Code:
struct Node {
    int* ptr;
    struct Node* next;
}

ptr is then de-referenced to whatever it is you were storing in it thus:

for int:

Code:
realValue = (int)*ptr;

or for a string:

Code:
char* string = (char*) *ptr;

In that way you can store anything you like in the linked list (and more to the point DIFFERENT things). You just need to remember what's where or have an appropriate test (or a marker to indicate what you are storing).

GL
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
remember that a pointer in C is simply an integer, storing an address in memory. This is why 32 bit architectures have such severe memory limits, as the maximum addressable space (without magic tricks) is 2^32 bits. On a 64bit cpu, a C int will be 64 bits wide thus giving you shitloads more addessable space to play with.
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
Code:
...
...
...
char value[10];

struct node
{
	char data;
	struct node *next;
}; typedef struct node *listpointer;

void main()
{
	listpointer list;
	list = NULL;
	list = (listpointer) malloc (sizeof (struct node));
	printf("Please enter an initial value");
	gets(value);
	strcpy(list->data, value);
	printf("\nFirst data item is %s\n", list -> data);
}

Doing my head in (other code omitted) - Got loaaadds more code, just mner =< *Tutors are no help*
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
should be char* data; not char data. unless you want to store just one byte?
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
Chilly, cheers.

\o/ That, plus allocating memory (I forgot to:() to my data value got it working \o/ Hooorah! Now time to write the rest of my program :D and then get a sexx0r 2.1/first.
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
aannnd I'm back.. :p

Code:
void add( struct node *new )
{
   if( head == NULL ) 
       head = new; 
   end->next = new;
   new->next = NULL; 
   end = new;   
}

If I kill "end->next = new;" and "end = new;" I can put in 1 new entry, however, if I leave them it.. I get a debug error come up.

How I use it:
Code:
	if(strcmp(value, arrive) == 0)
	{
		   printf("Enter in name -- ");
                   scanf("%s", data );
                   ptr = initnode(data);
                   add(ptr);
	}
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,047
not very impressed with your use of global variables there, but never mind that for now (but tend to avoid using globals in the future). Because of your use of globals I cant tell exactly whats going on in the rest of your code. What's the error (a segfault?)?
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
"An unhandled win32 exception" =<

http://pastebin.com/m6cbb41d7

I know it's messy, but it'll be fixed the second I can get more than 1 node running.. I actually stuck it in my code formatter, but it formatted in the way I didn't want.. Grrr.. *Will redo later* (Not allowed to use switches, before you ask)
Also, I've left a few old variables in there, stuff I've just not removed yet. -_-
 

Overdriven

Dumpster Fire of The South
Joined
Jan 23, 2004
Messages
12,758
Solved that issue.. Am working on the rest now, XD.. 5am coding is win,
 

Users who are viewing this thread

Top Bottom