Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8356001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:02:37+00:00 2026-06-09T10:02:37+00:00

typedef struct Data* DATAS; struct Data { char *name; char *city; DATAS next; };

  • 0
  typedef struct Data* DATAS;

  struct Data {
      char *name;
      char *city;
      DATAS next;
  };
  typedef struct Data DATA;

  int main(void){
     DATAS tmp;

     tmp=(DATAS) malloc(sizeof(DATA));   
     printf("please enter name:\n");
     scanf("%s",&tmp->name);
     printf("%s\n",&tmp->name);
     printf("please enter city:\n");
     scanf("%s",&tmp->city);
     printf("%s\n",&tmp->name);
     printf("%s\n",&tmp->city);

  return 0;
  }

This is part of a homework assignment. Or rather the concept is. I need to use ‘typedef struct Data* DATAS;’ which is throwing me off. When I run this I overwrite name with part of city so I get this as a result.

please enter name:
name
name
please enter city:
city
namecity
city

Any help would be great. Thanks. I’ve tried different variants of malloc using

tmp=(DATAS) malloc(sizeof(DATA));
tmp=(DATA) malloc(sizeof(DATA));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:02:39+00:00Added an answer on June 9, 2026 at 10:02 am

    The best way to allocate a struct Data is:

    struct Data *tmp;
    tmp = malloc(sizeof *tmp);
    if (tmp == NULL) {
        /* malloc failed, abort or take corrective action */
    }
    

    Defining a typedef for a pointer type is not recommended (at least by me); using struct Data * explicitly makes it clearer to the reader that you’re dealing with a pointer.

    Defining a typedef for a struct type is also unnecessary. A typedef simply declares a new name for an existing type; your type already has a perfectly good name, struct Data. Admittedly you have to type the struct keyword repeatedly, but that’s not really a problem.

    Casting the result of malloc is unnecessary; malloc returns a void* result, which can be implicitly converted to your pointer type. The cast can hide errors, such as forgetting the required #include <stdlib.h>.

    But these are style issues. Your current code:

    tmp=(DATAS) malloc(sizeof(DATA));
    

    is ok, and it should work. The problem is later in your code.

    scanf with a "%s" format expects a char* argument. You're giving it the *address* of achar* object, i.e., a value of type char**. The compiler won’t necessarily warn you about this. So this:

    scanf("%s",&tmp->name);
    

    should be:

    scanf("%s", tmp->name);
    

    But that’s still a problem, since tmp->name is an uninitialized pointer. It probably points to some random location in memory, and the call attempts to store data at that location. Or it could hold an invalid address, causing a crash. The behavior is undefined.

    You need to allocate space to hold the name, and cause tmp->name to point to it. You probably need another malloc() call here.

    So how much space do you need to allocate? Well, there’s no good answer to that, because scanf("%s", ...) places no limit on how many bytes it will read. However big the allocated space is, you can overflow it if you enter enough data.

    You probably don’t need to worry about that just yet; just keep it in mind for the future. For now, you can allocate “enough” space (say, 100 bytes) and be careful not to enter too much data. That should be enough to get your program working. (Check the documentation for scanf, and think about using something like "100s".)

    And keep in mind that scanf("%s", ...") reads a whitespace-delimited input string; if you type “John Doe”, it will only read the “John”, leaving ” Doe” for the next input operation.

    (I hope this isn’t too overwhelming.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have a structure typedef struct store { char name[11]; int age; } store;
#include <stdio.h> #include <stdlib.h> typedef struct { char *Name; int grade; int cost; }Hotel;
I declare typedef struct LABELS_STRUCT { char *name; int address; } `LABELS_STRUCT;` LABELS_STRUCT labels_array[ARRAY_LABELS];
I've got something like: typedef struct Data_s { int field1; int field2; } Data;
this is my struct typedef struct { char mmsi[10]; char name[20]; double latitude; double
I have a struct typedef struct { int id; string name; string address; string
I have this struct typedef struct grade { char firstName[SIZE]; char lastName[SIZE]; int stuNum;
Here is the code #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> typedef struct std_thread { char name[20];
55 typedef struct pidmap { 56 atomic_t nr_free; 57 void *page; 58 } pidmap_t;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.