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 8124179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:23:12+00:00 2026-06-06T06:23:12+00:00

I ask for d->id to put it on the list, but if I ask

  • 0

I ask for d->id to put it on the list, but if I ask for more than one, all the list items get the last one.

For example, if the for has 2 iterations and novo->prato[0] is "M1" (this is asked to the user) and novo->prato[1] is "M3", both positions stay "M3". Why?

Here is the code:

typedef struct pedido pedido, *ppedido;
typedef struct prato prato, *pprato;

struct pedido{
    char id[5];
    int prioridade;
    int mesa, n_pratos;
    pprato prato[TAM];
    ppedido prox;
};

struct prato{
    char id[5];
};

int verifica_prato(ppedido p,int j)
{
    FILE *f;
    struct item aux;

    int i=0;

    f = fopen("menu.bin", "rb");
    if(f == NULL){printf("O sistema nao consegue aceder a memoria");return;}

    while((fread(&aux, sizeof(struct item), 1, f)) == 1){
        if((strcmp(p->prato[j]->id, aux.id)) == 0)
            i++;
    }
    fclose(f);

    if(i == 0){
    printf("Prato nao existe no menu.\n");
    return 1;
        }

    if(i > 1)
    return 0;
}


for(i=0;i<novo->n_pratos;i++){
        do{
            printf("ID do prato %d: ", i+1);
            scanf("%s", &d->id);
            novo->prato[i] = d;
            k = verifica_prato(novo,i);

        }while(k != 0);
    }
  • 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-06T06:23:13+00:00Added an answer on June 6, 2026 at 6:23 am

    You are assigning the pointer to structure d when you do novo->prato[i] = d ,
    so each time you make a change to d it would be reflected at novo->prato[i] since all values in prato array is pointing to the same address i.e. d.

    I’m not exactly sure what you intend to do but one way could be to malloc d every time in loop before asking the user for input. Remember you also need to look into ‘freeing’ the memory prato[i] eventually.

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

Sidebar

Related Questions

I ask myself how use multiple images in one. For example youtube -> http://s.ytimg.com/yt/img/master-vfl102488.png
I'm trying to get a list of related articles. Example $title = $fetch_content[article_name]; //
I'm sorry if this question is a dumb one, but I must ask. In
I just wanna ask if there's a way where I could put an object
I will put my code in a simple way, but if any good soul
I m using one-to-many mapping for List using join table. There are lot of
hi im having difficulity with my machine when i ask for my gem list
in this program I'm trying to shuffle a list by randomly choosing two items
Update Just for future reference, I'm going to list all of the statistics that
I've got some code that will generically get all Controls in a form and

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.