So, I've been working on an assignment where I have preset headers provided by the professor, and it's been a struggle due to the relative newness of the material.
Here's the first header we were given:
data.h
Code:
typedef struct {char *key; int data;} Node;
void print_node(Node node);
char *key_dup(char *key);
We are expected to fill out data.c. Mine looks like this, and I am almost certain I am completely on the wrong track:
data.c
Code:
#include <stdio.h>
#include <string.h>
#include "data.h"
// Input: ’node’: a node
// Effect: node.key is printed and then the node.data is printed
void print_node(Node node)
{
printf("%s", node -> key);
printf("%d", node.data);
}
// Input: ’key’: a string ends with ’\0’
// Output: a pointer of type pointer to char, pointing to an allocated memory containing a copy of ’key’
// Effect: dynamically allocate memory to hold a copy of ’key’
char *key_dup(char *key)
{
char *duplicate;
duplicate = (char*) malloc(sizeof(*key));
strcpy(*duplicate, *key);
return duplicate;
}
I just want to fix any mistakes in my understanding of headers, pointers, and structures. These are all new concepts to me given my past course knowledge is in python and Java.
Thanks, in advance! In the meantime, I'll keep studying my textbook in hopes that I'll figure it out