#include <stdio.h>
#include <malloc.h>
typedef struct Point2D
{
int inint;
char inchar[10];
};
void structinput(struct Point2D *p, int su) {
int count = 1;
for (int i = 0; i < su; i++)
{
printf("%dst structure integer input: "", count);
scanf_s("%d", &p[i].inint);
printf("%dth structure string input: ", count);
scanf_s("%c", &p[i].inchar, sizeof(p[i].inchar));
count++;
//st[i].total = st[i].kor;
}
}
void structprint(struct Point2D *p, int su) {
int count = 1;
printf("--------------- Structural Results -------- \n");
for (int i = 0; i < su; i++)
{
printf("%dstructure: %d,\t%s\n", count, p[i].inint, p[i].inchar);
count++;
}
return 0;
}
int main(void) {
int su;
printf ("Enter number of structures :");
scanf_s("%d", &su);
struct Point2D *p /*= { 0 }*/;
p = (struct Point2D *)malloc(sizeof(struct Point2D)*su);
structinput(&p, su);
structprint(&p, su);
}
You can enter how many structures you want to create and create them I wanted to send it as a function to pay back, input it, and print it out as a function.
There is a corruption in the input related to the pointer, but I don't know why, so I'm asking you this question.
Thank you. ;
;; c visual-studio
There are many things to check...If you write it down as easily as possible...
Once the char array has been declared in advance, it should be copied and inserted as a function of strcpy.
I don't know if you're learning pointer operations, but first understand it like (p+1)->int
.
It should be structinput(p,su); not structureinput(&p,su);
https://wandbox.org/
Connect to the above site and test it with the code below.
#include <stdio.h>
#include <malloc.h>
struct Point2D
{
int inint;
//char inchar[10];
};
void structinput(struct Point2D *p, int su) {
int count = 1;
for (int i = 0; i < su; i++){
printf("%dst structure integer input: "", count);
//scanf_s("%d", &p[i].inint);
(p + i + 1)->inint = count;
count++;
//st[i].total = st[i].kor;
}
}
int main() {
int su = 5;
struct Point2D *p /*= { 0 }*/;
p = (struct Point2D *)malloc(sizeof(struct Point2D)*su);
structinput(p, su);
printf("%d\n", (p + 1)->inint);
printf("%d\n", (p + 2)->inint);
printf("%d\n", (p + 3)->inint);
printf("%d\n", (p + 4)->inint);
printf("%d\n", (p + 5)->inint);
}
It is recommended to declare all variables in the main statement at the top.
If you have allocated memory using malloc, you have to release it using free.
In addition, the structure point2D *p (pointer) is received as a factor in the structure input function.
If you look at structure input (&p, su), it's passing the address of the pointer.
I think there will probably be an error there.
© 2024 OneMinuteCode. All rights reserved.