Thread: Understanding Pointers, Structures, & Headers - BST

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    27

    Understanding Pointers, Structures, & Headers - BST

    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

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Most of your mistakes just seem to be typos.

    1) you should really only use the arrow operator if node was a pointer (it's not). The arrow operator is shorthand for (*node).key -- in this case, regular dot should be fine.

    2) it would be nice to add some white space to your print function.

    3) you need some checks. And try to remember that *ptr is the object being pointed to (a single char). You want access to the whole string.

    4) sizeof and strlen() will give you very different numbers as well.
    Code:
    char *key_dup(char *key)
    {
       char *dup = NULL;
       if ((dup = malloc(strlen(key)+1)) != NULL) 
       {
           strcpy(dup, key);
       }
       return dup;
    }

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    27
    @whiteflags

    Thanks for the tips! Yeah, the check makes sense, and I brainfarted on strlen vs. sizeof (

    And I think I understand it better now. I use arrows (->) for pointers to the structure itself, to access the elements within it?

    Here's where I stand atm:

    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);
        // not sure if this is what you meant by whitespace, is it just extra lines between the code?//
        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 = NULL;
        if ((duplicate = malloc(strlen(key)+1)) != NULL)
            strcpy(duplicate, key)
        return duplicate;
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And I think I understand it better now. I use arrows (->) for pointers to the structure itself, to access the elements within it?
    Exactly.

    Looks better now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Understanding Headers
    By AeonMoth in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2007, 05:53 AM
  3. Help understanding Headers and Other beginner stuff
    By Saintdog in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2004, 09:34 PM
  4. Understanding Headers and Inclusions
    By jdm in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 10:11 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread