ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a function for deleting the node in a list, once it searches for a string, but before it is deleted and freed from memory, the contents of the node are copied into the parameter QUIZ *deletedQuestion, but I'm getting the following compiler error with the strcpy function and im not sure why:
Code:
list.c: In function `Delete':
list.c:122: error: request for member `question' in something not a structure or union
list.c:123: error: request for member `answer' in something not a structure or union
list.c:169: error: request for member `question' in something not a structure or union
list.c:170: error: request for member `answer' in something not a structure or union
Code:
int Delete (NODEPTR *headPtr, char *target, QUIZ *deletedQuestion)
{
/* Node pointers that reference the previous node
and the current node in the list. */
NODEPTR prev = NULL, curr = NULL;
/* Check to see if the list is empty. */
if (IsEmpty (*headPtr))
{
/* Display an appropriate message indicating that
deleting from the list was not accomplished
because the list is empty. */
fprintf (stderr, "Unable to delete from an empty list.\n\n");
/* If the list is empty, return the integer value
of -1, indicating that the question, char
*target, was not successfully deleted from the
list. */
return (-1);
}
/* Traverse the list until the target value is found. */
/* Initialize the node pointer referencing a previous
node in the list to NULL. */
prev = NULL;
/* Set the node pointer referencing a current node to
the head of the list. */
curr = *headPtr;
/* First check to see if the question, char *target,
that we want to delete is stored in the first
node in the list, because this would require
*headPtr to be modified. */
if (strcmp (curr -> data.question, target) == 0)
{
/* Copy the contents of the data portion of the
node containing the question, char *target,
that we want to delete from list into the
QUIZ structure, deletedQuestion. */
strcpy (curr -> data.question, *deletedQuestion.question);
strcpy (curr -> data.answer, *deletedQuestion.answer);
/* If the question, char *target, is stored in the
first node in the list, change the head of the
list to start at the node pointer referencing
the current node's next pointer. */
*headPtr = curr -> next;
/* Delete and free the memory that was being used
to store the question, char *target, in the
first node of the list. */
free (curr);
/* If the question, char *target, was freed and
deleted, return the integer value of 1,
indicating that the question was successfully
deleted from the list. */
return (1);
}
/* Otherwise, traverse through the list to find the
question, char *target, to be deleted. */
else
{
/* Traverse the list if the node pointer
referencing the current node in the list is not
equal to NULL and the node pointer referencing
the current node in the list is not storing the
question, char *target, in the data portion of
its node. */
while ((curr != NULL) &&
(strcmp (curr -> data.question, target) != 0))
{
/* Traverse the list. */
prev = curr;
curr = curr -> next;
}
/* If the node pointer refencing the current node in
the list not equal to NULL, then the question,
char *target, was found. */
if (curr != NULL)
{
/* Copy the contents of the data portion of the
node containing the question, char *target,
that we want to delete from list into the
QUIZ structure, deletedQuestion. */
strcpy (curr -> data.question, *deletedQuestion.question);
strcpy (curr -> data.answer, *deletedQuestion.answer);
/* Change the next pointer of the previous node,
in order to avoid a broken list. */
prev -> next = curr -> next;
/* Delete and free the memory that was being used
to store the question, char *target, in this
node of the list. */
free (curr);
/* If the question, char *target, was freed and
deleted, return the integer value of 1,
indicating that the question was successfully
deleted from the list. */
return (1);
}
/* Otherwise, the question, char *target, was not
found in the list. */
else
{
/* If the question, char *target, was not found
in the list, return the integer value of -1,
indicating that the question was not
successfully deleted from the list. */
return (-1);
}
}
}
Can anyone tell me why I can't copy the contents of the data portion.question of the current node into the parameter - QUIZ structure pointer?
My structures are defined as:
Code:
/* NODEPTR is defined as an alias for the structure of type
node, so that it can be used within the structure before
it is completely defined. */
typedef struct node * NODEPTR;
/* This struct, typedefed to be of type quiz, is meant to
store all the data about each question in the quiz that
will be given to the user. */
typedef struct quiz
{
char question [NUMBEROFQUESTIONCHARACTERS];
char answer [NUMBEROFANSWERCHARACTERS];
} QUIZ;
/* A NODE is a structure that allows us to build a linked
list since it has both a data portion and a NODEPTR as
its members. The NODEPTR called next is known as the
link. */
typedef struct node
{
QUIZ data;
NODEPTR next; /* OR struct node *next; */
} NODE;
Why are you mixing different methods to access data?
For instance:
Code:
curr is defined as a NODEPTR (or rather: struct node *)
deletedQuestion is defined as a QUIZ *
Both curr and deletedQuestion are pointers to structs, but then you do this:
strcpy (curr -> data.question, *deletedQuestion.question);
Why switch up the syntax? This is another ambiguity problem. With curr, you use the arrow operator, telling the computer to dereference curr and then access data.question. For deletedQuestion, you slap a star to dereference, but what is being dereferenced? The whole thing (deletedQuestion.question)? Use the arrow operator or use parentheses to clarify the order of dereferencing such as:
Yes, I am sure that the list contains the right data.
I did not allocate memory for the QUIZ structure, it is just declared in main as QUIZ deletedQuestion;
When I put print statements in the function, it doesn't get to them, such as in the conditions of the question being the first node in the list and it being traversed, but they are never printed (I don't believe). The function does delete properly and return the correct value. What do you think?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.