Thread: Accessing a data element in a function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Exclamation Accessing a data element in a function

    I cannot for the life of me figure out how to get stuff out of my array that i pass to my function(listSort()). all i want for example is data[0] in my function that i called. i can figure out the rest of what i'm supposed to do if i can do this. any help is greatly appreciated! (this is for a linkedList sort program, so i cant do a simpler array type)

    main.c
    Code:
    [tag]
    #include <stdio.h>
    #include <time.h>
    #include "list.h"
    
    #define ARRAY_SIZE 4
    
    
    int main() {
    	
        double data[] = {9,3,4,2};
        int i;
    
        listSort(&data);
    
        for (i = 0; i < ARRAY_SIZE; i++)
           printf("%g\n", data[i]);
    }
    [/tag]
    listSort.c
    Code:
    [tag]
    #include <stdio.h>
    #include <assert.h>
    #include "list.h"
    
    void listSort (struct linkedList * lst){
    	double u;
    
                    u = lst[0]; //<----------------------------here is what i cant figure out. how do i access the data in lst???
    
    	printf("%g\n", n);
    [/tag]

    i dont know if my .h file matters.... but i'll include it anyways i guess. (i have all these already coded)
    list.h
    Code:
    list.h[tag]
    #ifndef _list_
    #define _list_
    
    #ifndef Eletype
    #define EleType double
    #endif
    
    
    
    struct dlink{
    	EleType value;
    	struct dlink * next;
    	struct dlink * prev;
    };
    
    struct linkedList{
    	int size;
    	struct dlink * frontSentinel;
    	struct dlink * backSentinel;
    };
    
    void listSort (struct linkedList * lst);
    
    void LinkedListInit (struct linkedList *q);
    void linkedListFree (struct linkedList *q);
    void linkedListAddFront (struct linkedList *q, EleType e);
    void linkedListAddBack (struct linkedList *q, EleType e);
    void linkedListRemoveFront (struct linkedList *q);
    void linkedListRemoveBack (struct linkedList *q);
    int linkedListIsEmpty(struct linkedList *q);
    void _addLink(struct linkedList *q, struct dlink *lnk, EleType e);
    void _removeLink(struct linkedList *q, struct dlink *lnk);
    EleType linkedListFront (struct linkedList *q);
    EleType linkedListBack (struct linkedList *q);
    
    #endif
    [/tag]
    i've looked at over a dozen tutorials and example code, everything is posted in a single .c file(or .cpp), i need to put these in 2 different ones. thanks for your time!
    Last edited by Zooker; 02-04-2009 at 05:00 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    lst[0] is not an int; it is a linked-list-node kind of a thing. So you would use dots just like everywhere else.

    Or, I guess reading it, your function shouldn't take a struct Linkedlist thing, but an array of doubles.
    Last edited by tabstop; 02-04-2009 at 05:37 PM.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I cannot for the life of me figure out how to get stuff out of my array that i pass to my function
    Well, if data is an array, to access the ith element of it, you just need to use "data[i - 1]". But then, that's pretty basic.

    I don't know what you are trying to do, but you look a bit lost. You are passing a variable of type double[4] (who gets implicitly cast into a double *) to a function taking a struct linked list *. There should be a bell ringing in your head, if it's not in your compiler.
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    what do you meand dots tabstop?

    i totally see what you mean about the doubles foxman. sorry this is incredibly elementary, it's probably why i cant find it anywhere. but anyways i changed the array type and i still cant get the elements out of data. I was thrown into C from java so i'm having trouble with syntax. I am trying to make a doubly-linked array going forwards and backwards with sentinels for a sorting algorithm i am to make.

    Code:
    int main() {
    	
        struct linkedList lst[] = {9,3,4,2};
       
    
        listSort(&lst);
    
        for (int i = 0; i < ARRAY_SIZE; i++)
           printf("%g\n", lst[i]);
    }
    i cannot get lst[#] to work. i get garbage numbers. i have also tried:
    lst->frontSentinel->next;
    list[0];
    &list[0];

    Code:
    void listSort (struct linkedList * lst){
    	double t, v, u;
    	struct linkedList x, stk;
    
    	printf("%g\n", lst[0]); //<-----this will not print "9". it prints 2.64193e-308.
    i know i'm doing something stupid wrong. thanks a ton for your guys time!

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are initializing your linked list as if it is an array of ints

    do you really want and array of ints?

    according to your function prototype lst[0] is a struct - there is no format in printf to print a whole struct

    %g format is used to print float numbers in scientific notation so why do you expect to see "9"?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You use dots to access members of a structure object (I'm pretty sure this is exactly equivalent to the use of dots in Java). You have to decide whether you have an array of ints, an array of doubles, or an array of some mystical "struct linkedList".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM