![]() |
| | #1 |
| Registered User Join Date: Oct 2006
Posts: 23
| Structure and Linked List User Input Question My basic problem is the understanding of how do use pointers and store them in a structure. Thanks in advance for any help given. PHP Code: |
| kevndale79 is offline | |
| | #2 |
| Fear the Reaper... Join Date: Aug 2005 Location: Toronto, Ontario, Canada
Posts: 625
| Ok, so you're not looking at a Linkedlist correctly. Here's how you should look at it : Code: value 1
next -----> value 2
next ------> value 3
next ----> and so on
Keep in mind, though, that what you're doing at the moment doesn't set up a linked list structure either. For the purpose of the question, I could see yourself simply modifying the structure to this : Code: struct oomba
{
int value[5];
struct oomba *next;
};
__________________ Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction |
| Happy_Reaper is offline | |
| | #3 |
| Registered User Join Date: Oct 2006
Posts: 23
| Revised Program No Luck Still working out the kinks. This gives me lots of errors, especially the storage of the values not reflecting the proper struct union type. I only wish my book was a little more helpful. Thanks, Kevin PS- Any additional advice would be greatly appreciated including a good faq or walkthrough of writing a program similar to this to let me know the basic format and what I am missing/ doing wrong. Thanks again for all the help PHP Code: |
| kevndale79 is offline | |
| | #4 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Well, it looks like you're trying to declare first twice, and then you assign it to point to an integer (even though it's a pointer to a structure!). val1, val2 etc are ints but you're treating them as structures. [edit] And assuming you know that value is an int, you're missing an &: Code: scanf("%d",&value);
Perhaps you'd like to have a look at this program: Code: #include <stdio.h>
struct numbers {
int num[6];
};
void get_numbers(struct numbers *n);
int main(void) {
struct numbers n;
int x;
get_numbers(&n);
for(x = 0; x < 6; x ++) {
printf("%i\n", n.num[x]);
}
return 0;
}
void get_numbers(struct numbers *n) {
int x;
for(x = 0; x < 6; x ++) {
printf("Enter number %i: ", x+1);
scanf("%i", &n->num[x]); /* no error checking */
}
}
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #5 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Try copying and pasting your code. If this is actually your code, then you have numerous problems. It helps to list the errors you're actually getting, so people don't have to waste time copying / compiling your broken code just to find out what's wrong. Code: int main()
{
int value, val1, val2, val3, val4;
oomba *first
2 - You don't have a ; at the end of this line. Code: printf("\n\nPlease input the value 5");
scanf("%d",val4.value);
Oomba *first;
Code: first = &value; I think you're misunderstanding what the purpose of a linked list is. You have room in one structure for six integers, so you don't need a list. I suspect your list structure is designed wrong, or you're misunderstanding the purpose. I suspect you want one integer per structure, so that you hold one number per list element. Then you make a new node for each number. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #6 |
| Registered User Join Date: Oct 2006
Posts: 23
| new and improved but still not working Thanks for the sample program. I changed the structure to a different format (array) hoping that might alleviate some of my many problems. This program does compile but will not run. PHP Code: |
| kevndale79 is offline | |
| | #7 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Why are you using an array? Do you want a linked list, or do you want an array? Why is there a pointer to an integer in your linked list now? Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #8 |
| Registered User Join Date: Oct 2006
Posts: 23
| Linked List/ Array I was hoping to gain some understanding of what i was doing and hoping by getting an array to work that would help me figure out my multiple problems with the linked list. The linked list is what I actually want to do but I am experimenting trying to figure it out. Thanks, Kevin |
| kevndale79 is offline | |
| | #9 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Write one program that enters them into an array. Write one program that enters them into an array in a structure. Write one program that enters them into an array of structures, each structure containing only one int. Write another program that enters them into a linked list. Remember, your linked list is acting like an array in that instead of just walking through the array, you move through the linked list via pointer manipulation. Your array node should only store one integer. The integer itself should just be a plain old int inside a structure, not a pointer to an int, unless you feel like calling malloc for each node to allocate that integer also. Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? Last edited by quzah; 10-04-2006 at 06:02 PM. |
| quzah is offline | |
| | #10 |
| Registered User Join Date: Oct 2006
Posts: 23
| Back on track This is what I got, I think I am getting the idea but now I am getting an error C2059 with Microsoft visual basic that says syntax error 'type' for the lines that scanf. I didnt write the display function yet, still trying to get this part to work. PHP Code: |
| kevndale79 is offline | |
| | #11 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,640
| Code: printf("Please input the value 1\n");
scanf("%d",struct Oomba v1);
Code: printf("Please input the value 1\n");
scanf( "%d", &v1.value );
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #12 |
| Registered User Join Date: Oct 2006
Posts: 23
| Thanks finally got that working That part is working now THANKS! Now I am trying to create the function to print out the list of the linked list and it compiles and runs up until the point it sends the input to the function. Here is the updated code with the display function. PHP Code: |
| kevndale79 is offline | |
| | #13 |
| Fear the Reaper... Join Date: Aug 2005 Location: Toronto, Ontario, Canada
Posts: 625
| Code: void display(struct Oomba *contents)
{
while (contents != NULL)
{
printf("%s",contents->value);
contents = contents->nextaddr;
}
return;
}
__________________ Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction |
| Happy_Reaper is offline | |
| | #14 |
| Registered User Join Date: Oct 2006
Posts: 23
| Got it working So now my function and structure work with the input of the 5 values. The assignment me asks us to have the user input 5 values then print the results and then input one more value. I am trying to figure out how to get that to work. Should I write another functiion? PHP Code: |
| kevndale79 is offline | |
| | #15 |
| Registered User Join Date: Oct 2006
Posts: 23
| This is my attempt at inputting an additional var. This is my first try at getting the 6th value inputted. Compiles but crashes after the first 5 values are inputted and displayed. PHP Code: |
| kevndale79 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Following CTools | EstateMatt | C Programming | 5 | 06-26-2008 10:10 AM |
| help! Placement of nodes in a Linked List | lostmyshadow | C Programming | 6 | 12-17-2007 01:21 PM |
| Problem with linked list ADT and incomplete structure | prawntoast | C Programming | 1 | 04-30-2005 01:29 AM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| singly linked list | clarinetster | C Programming | 2 | 08-26-2001 10:21 PM |