0

I was working on a homework assignment and I can't understand how to return a pointer from a void function. This is what I have below. Can someone help me out or give me a hint at least? Thank you!

typedef struct Node {
    void *data;
    struct Node *next;
} Node;

/* List: Represents a linked list. */
typedef struct List {
    Node *head;
    int size;
} List; 

 *    Given: A pointer to a List structure
 *    Returns: The size of that list */
 43  void *lstget(List *lst, int idx) {
 42         Node *current = NULL;
 43         int count;
 44         bool found = false;
 45
 46         if(lst->head == NULL || idx > lst->size || idx < 0)
 47                 return NULL;
 48         current = lst->head;
 49
 50         while(!found) {
 51                 if(count == idx) {
 52                         bool found = true;
 53                         /* THIS IS WHERE I WANNA DO IT */  
 54                 }
 55                 current = current->next;
 56                 count++;

I was thinking to so something like this, but I am not sure if it would work:

*lst = *((Node *)current->data)
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • Welcome to SO. Where is the "void function" you mention? Your code is incomplete. Please provide the full function you have so far and tell us what is blocking you and/or what was wrong when you tried your solution. – Gerhardh Mar 16 '20 at 06:17
  • 2
    A void function has return type `void`. Your function has return type `void *`, which is not the same thing. – Barmar Mar 16 '20 at 06:26
  • Are you sure it's supposed to return the size of the list? You can do that with `return lst->size`, and it doesn't need to be a pointer. – Barmar Mar 16 '20 at 06:32
  • Maybe you are lookijg for this question: https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c – Jabberwocky Mar 16 '20 at 08:21
  • @Barmar Yes, that is what I was actually confused about. From what I understand, void has no return type. And, void* is a generic data pointer. My void *lstget - supposed to return a pointer of the element that I am getting. The problem is, I don't know where I need to assign the pointer to be accessed. For example, should I create a new pointer, or change the idx value with the element's value, and return a pointer to it. I don't know... Thank you! – mansur ischanov Mar 16 '20 at 17:32
  • It depends on what you're supposed to be returning a pointer to. The question is not clear about that. "return the size of that list" doesn't seem to require a pointer at all. – Barmar Mar 16 '20 at 17:45

1 Answers1

0

I suggest this:

struct Node {
    void* data;
    Node* next;
}*head = NULL;

/*struct List {
    Node* head;
    int size;
}List1;*/
//I don't recommend this you can use a function to return list size

int length() {// return list size
    int length = 0;
    struct Node* current;

    for (current = head; current != NULL; current = current->next) {
        length++;
    }

    return length;
}

void* lstget(Node* lst, int idx) {
    Node* current = NULL;//no need for this you can (Node* current = lst->head) from first
    int count=0;
    bool found = false;
    int size = length();
    if (lst == NULL || idx > size || idx < 0)
        return NULL;
    current = lst;

    while (!found) {
        if (count == idx) {
            bool found = true;
            Node* tmp = (Node*)malloc(sizeof(Node));
            tmp = current;
            return tmp;
        }
        current = current->next;
        count++;
    }
}

in your main function you can have returned node like this:

    Node* tmp;//you can malloc too
//Node* tmp=(Node *)malloc(sizeof(Node)) but most likely no need
    tmp = (Node*) lstget(head, 1);
hanie
  • 1,863
  • 3
  • 9
  • 19
  • Thank you for breaking it down. I never knew that I could return from void* function as you are returning tmp. – mansur ischanov Mar 16 '20 at 17:39
  • @mansurischanov There is no need for thanks ,but instead of posting "thank you" in stack overflow you'd better do what is written here(https://stackoverflow.com/help/accepted-answer) – hanie Mar 16 '20 at 18:27