Thread: Struct.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    62

    Struct.

    For data insertion in a struct should I put " " or ' '?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your question does not make sense. Perhaps you should write a test program to illustrate the options you're considering.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    I have two structs and also a self-referential struct which has as members the other two. I have to create a circular linked list with the last struct. Here are the structs.

    Code:
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        char area;
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death;
        char location_description;
        location coordinates;
        char URL;
        struct incList *next;
    }incident;
    The chars must be strings with letters. the ints just numbers. Each data must be separated with semicolon(. How do I do that?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The chars must be strings with letters.
    First thing I notice is that your structures contain no "strings" just some doubles, integers, and single character types.

    Each data must be separated with semicolon(. How do I do that?
    Second there are no "separators" involved in the structures, perhaps when you output the data held in the structures you need to print a "separator" character after the data.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you realize that "char URL;" can only hold a single character?

    Tim S.
    "...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

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    The last two replies don't help me at all.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    And, your reply shows that you can not be helped by me.

    Goodbye, I hope you never find a programming job with your lack of ability and laziness.

    Tim S.
    "...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

  8. #8
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    And I am sure of the fact that you know (because I think that you are not stupid) that there aren't only 2 people on the Internet. If you cannot help me with that :
    1) That means that there are other people in that forum who can help
    2) You have little to none experience with coding and
    3) Your patience is non-existent.

    I hope this forum and coding is only hobby of yours.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The last two replies don't help me at all.
    Why not? Explain yourself, your question really doesn't make much sense. If you want more coherent answers you need to provide more coherent questions.

    Do you understand the difference between a character and a string? Your structure definitions strongly hint that you have no idea about the differences.

    It would be better if you posted a small complete program that illustrates your problems. Also show what you are inputting into said program and what you are outputting from said program.

  10. #10
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    I know the difference between a char and a string but I forgot to make in the previous part of a code the chars into arrays. Here is the new part.

    Code:
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        /*Gia string ""*/
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    }incident;
    I have two problems.
    1)That this is the first part of the code so I don't have much of the code done.(Pretty much none)
    2)How do I make the last struct (that uses self-reference) a circular linked list that makes 10 incidences and put them in a .txt file and print them?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Circular linked lists are a bit of a pain, especially if you care about element order.
    Linked list - Wikipedia

    A simple example.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    typedef struct node {
      int data;
      struct node *next;
    } node_t;
    
    node_t *insert(node_t *list, int data) {
      node_t *new = malloc(sizeof(*new));
      if( list == NULL ) {
        list = new;
        new->next = new;
      } else {
        new->next = list->next;
        list->next = new;
        new->data = data;
      }
      return list;
    }
    
    void print(node_t *list) {
      node_t *temp = list;
      if ( temp != NULL ) {
        do {
          printf("Node=%d\n", temp->data);
          temp = temp->next;
        } while ( temp != list );
      }
    }
    
    int main ( ) {
      node_t *list = NULL;
      for( int i = 0 ; i < 5 ; i++ ) {
        list = insert(list, i);
      }
      print(list);
    }
    
    $ gcc bar.c
    $ ./a.out 
    Node=0
    Node=4
    Node=3
    Node=2
    Node=1
    Notice that the first node is always at the head of the list, and the remainder appear in reverse order.

    If you want to preserve the order, then the insert() function needs to do some kind of 'find the end' operation beforehand.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    That this is the first part of the code so I don't have much of the code done.(Pretty much none)
    You need to show some actual effort, no one here is going to just give you the solution.

    How do I make the last struct (that uses self-reference) a circular linked list that makes 10 incidences and put them in a .txt file and print them?
    Break the problem down to manageable chunks, don't try to solve the entire program in one go. I'd start by working on the linked list, there are plenty of explanations available online, and probably in your text book as well to at least get you started.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2019, 07:51 AM
  2. typedef struct vs struct declaration error in code
    By shaswat in forum C Programming
    Replies: 3
    Last Post: 07-25-2016, 06:45 AM
  3. Replies: 5
    Last Post: 06-30-2011, 03:24 PM
  4. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  5. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM

Tags for this Thread