Thread: printing struct data in a function, while calling using pointers

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    Post printing struct data in a function, while calling using pointers

    hey guys im realy new to programming but am learning fast :) although i think i am going about this the wrong way, im trying to pass information to a function to print structure, i want to use malloc because later on i want to change the code so the user inputs how many "players" and i want to use pointers because we are just starting to learn that at uni,
    Code:
    //structure.h
    
    struct pointsscored
    {
        char name[20];
        int goals;
        int behinds;
    };
    
    //main.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "structure.h"
    void print_data(void *ptr);
    
    
    int main()
    {
        struct pointsscored *ptr;
        int i, numofplayers = 3;
    
    
        ptr = (struct pointsscored*) malloc(numofplayers * sizeof(struct pointsscored));
    
    
        for(i=0;i<numofplayers;++i)
        {
            printf("Enter name, goals and behinds of player %d (use the space bar to seperate)\n", i+1);
            scanf(" %s%d%d", &(ptr+i)->name, &(ptr+i)->goals, &(ptr+i)->behinds);
        }
    
    
        for(i=0;i<numofplayers;++i)
        {
            print_data(ptr);
        }
    
    
    
    
        system("pause");
        return 0;
    }
    
    
    void print_data(void *ptr)
    {
        printf("%s" , ((char)ptr->name));
    }
    i know im passing to the function wrong so any help at all with anything in my code will be much appreciated. thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For simplicity, I am just going to put everything into a single source file.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct pointsscored
    {
        char name[20];
        int goals;
        int behinds;
    };
    
    void print_data(void *ptr);
    
    int main()
    {
        struct pointsscored *ptr;
        int i, numofplayers = 3;
    
        ptr = (struct pointsscored*) malloc(numofplayers * sizeof(struct pointsscored));
    
        for(i=0;i<numofplayers;++i)
        {
            printf("Enter name, goals and behinds of player %d (use the space bar to seperate)\n", i+1);
            scanf(" %s%d%d", &(ptr+i)->name, &(ptr+i)->goals, &(ptr+i)->behinds);
        }
    
        for(i=0;i<numofplayers;++i)
        {
            print_data(ptr);
        }
    
        //system("pause");
        return 0;
    }
    
    void print_data(void *ptr)
    {
        printf("%s" , ((char)ptr->name));
    }
    Then compile with full warnings:

    Code:
    main.c||In function 'main':|
    main.c|23|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c||In function 'print_data':|
    main.c|37|warning: dereferencing 'void *' pointer|
    main.c|37|error: request for member 'name' in something not a structure or union|
    ||=== Build finished: 1 errors, 2 warnings ===|
    I would suggest not working with void pointers in your function parameters as a beginner. Just make your parameter an explicit pointer to the struct type.

    Some other notes:

    • Check that malloc succeeds before attempting to use that memory.
    • When using "scanf" to read a string, you shouldn't include the '&', as an array name by itself acts as a pointer to its first element.
    • You don't need to cast malloc: FAQ > Casting malloc - Cprogramming.com
    • Whatever memory you allocate, you should free.


    Here is a simplified example to illustrate the process:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct struct_str
    {
        char s[20];
    };
    
    void print_str(struct struct_str *test);
    
    int main(void)
    {
        struct struct_str *stest;
    
        stest = malloc(sizeof(*stest));  /* error checking omitted */
    
        strcpy(stest->s, "This is a test.");
    
        print_str(stest);
    
        free(stest);
    
        return 0;
    }
    
    void print_str(struct struct_str *test)
    {
        printf("%s\n", test->s);
    }
    Last edited by Matticus; 12-15-2017 at 08:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using data struct and I got an error in struct function
    By abrofunky in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2012, 07:47 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Typdef struct and function pointers
    By tempster09 in forum C Programming
    Replies: 1
    Last Post: 12-06-2009, 10:16 AM
  4. Replies: 2
    Last Post: 12-16-2006, 06:53 PM
  5. calling a function with a pointer to a struct
    By bomberto in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2006, 04:21 AM

Tags for this Thread