Thread: struct problem

  1. #1
    Registered User
    Join Date
    Jun 2019

    struct problem

    Hi All,

    Im very beginner at C but my teacher gave me an excercise and I can not do it. The task is to create a struct called "team". This struct contains a char: "char c", and an int: "int length" and a char array: "nam[10]. I have to write a function named "newteam". The input of the function is string:actually the name of the team.
    What this function should do is get the input string, and store it in the "length" and drop the first character of the string into the "char c".
    I have this code so far:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct team_struct *Teams;
    typedef struct team_struct{
     char f;
     char nam[10];
     int length;
    Teams newteam(char *nam){
        Teams s = malloc(sizeof(Teams));
        s->length = strlen(nam);
    int main() {
    How should I print out the values?

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    May 2009
    Your function newteam is missing a return statement.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2019
    Quote Originally Posted by stahta01 View Post
    Your function newteam is missing a return statement.
    so is main.
    newteam also has a superfluous semi colon after the closing bracket

  4. #4
    Registered User catacombs's Avatar
    Join Date
    May 2019
    You don't need to allocate memory on line 12. You can set the character array size on line 5.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    There is a sneaky little bug with "Teams" - Because it is a typedef'd pointer, you need to treat it a little different...

    The best way around this is to remove dependency from the type with the malloc

    Teams newteam(char *nam){
        // Change this...
        // Teams s = malloc(sizeof(Teams));
        // To this...
        Teams s = malloc(sizeof(*s));
        s->length = strlen(nam);
        strncpy(... /*hint hint*/
        return s;

    The obvious way to avoid problems like this is to not typdef a pointer and just use it as is
    typedef struct team_struct Teams;
    Teams *s = malloc(sizeof(*s));
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct in C problem
    By Nhân Trần in forum C Programming
    Replies: 5
    Last Post: 11-17-2015, 11:00 AM
  2. struct struct struct!!!
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 06-19-2010, 08:19 AM
  3. Struct problem
    By chr15 in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 02:37 PM
  4. Struct problem
    By totalfreeloader in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 09:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread