| |

VerySource

 Forgot password?
 Register
Search
View: 570|Reply: 2

Passing pointer parameter does not get the desired value

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-24 11:20:01
| Show all posts |Read mode
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"

#define MAX_PKT 1024

#define MAX_SEQ 7

typedef enum {FALSE, TRUE} boolean;

typedef unsigned int seq_nr;

typedef enum {dat, ack, nak} frame_kind;

typedef enum event_type {timeout, frame_arrival};

typedef struct
{frame_kind kind;
seq_nr seq;
seq_nr ack;
char info [1024];
} frame;
void from_network_layer (frame * r)
{
frame fra;
r =&fra;
printf ("Input information:");
memset (fra.info, 0, strlen (fra.info));
for (int i = 0;; i ++)
{
fra.info [i] = getchar ();
if ('\n' == fra.info [i])
{
fra.info [++ i] = '\0';
break;
}
}
}
void main ()
{
seq_nr ack = 0;
frame send, rev;
event_type event;
from_network_layer (&send);
send.kind = dat;
}
Why can't SEND get FRA.INFO formed from from_network_layer
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-10 11:45:02
| Show all posts
frame fra;
r =&fra;
fra is on the stack and disappears after the function exits
According to your use method in main (), you can change it like this:
void from_network_layer (frame * fra)
{
printf ("Input information:");
memset (fra-> info, 0, strlen (fra-> info));
for (int i = 0;; i ++)
{
fra-> info [i] = getchar ();
if ('\n' == fra-> info [i])
{
fra-> info [++ i] = '\0';
break;
}
}
}
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-10 14:30:01
| Show all posts
Thank you
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list