I've been working with C a little over the weekend and
experimenting with pointers. This is not a matter of life and death, I'd just like to know where my mistake is.
Code looks like this:
#include <stdio.h>
#include <stdlib.h>
struct asinine {
char *str[25];
int *p1;
int p2;
};
/*function protos*/
struct asinine *RetPt(void);
void printall(struct asinine *ptr);
int main(void) {
struct asinine *pret;
int j;
/*allocate memory for container structure*/
pret = (struct asinine *)malloc(sizeof(struct asinine));
/*test the function for a while*/
if (pret) {
for (j=1 ; j <=3 ; j++) {
pret = RetPt();
printall(pret);
}
}
/* all done*/
return 0;
}
struct asinine *RetPt(void) {
/* ptr struct to pass to printall and main */
struct asinine *ret;
char buf[25];
puts("String value for asinine: "
/*gets(buf);
if (strlen(buf) < 26) {
strcpy(ret->str,buf);
} no difference.*/
scanf(" %s", &ret->str);
/* str loaded*/
putchar('\n');
puts("First int value: "
scanf(" %d", &ret->p1);
/*first integer */
putchar('\n');
puts("Second, and last int value: "
/* second integer */
scanf(" %d", &ret->p2);
putchar('\n');
return ret;
}
void printall(struct asinine *p) {
/* prints struct members*/
printf("String value:%s ,is given in adjacency with %d , #1 and , %d, #2.\n",p->str,p->p1,p->p2);
}
A sample run looks like:
String value for asinine:
thisnthat
First int value:
12
Second, and last int value:
35
String value:èõÿ¿nthat ,is given in adjacency with 12 , #1 and , 35, #2.
Why the garbled chars?
Thanks.
experimenting with pointers. This is not a matter of life and death, I'd just like to know where my mistake is.
Code looks like this:
#include <stdio.h>
#include <stdlib.h>
struct asinine {
char *str[25];
int *p1;
int p2;
};
/*function protos*/
struct asinine *RetPt(void);
void printall(struct asinine *ptr);
int main(void) {
struct asinine *pret;
int j;
/*allocate memory for container structure*/
pret = (struct asinine *)malloc(sizeof(struct asinine));
/*test the function for a while*/
if (pret) {
for (j=1 ; j <=3 ; j++) {
pret = RetPt();
printall(pret);
}
}
/* all done*/
return 0;
}
struct asinine *RetPt(void) {
/* ptr struct to pass to printall and main */
struct asinine *ret;
char buf[25];
puts("String value for asinine: "
/*gets(buf);
if (strlen(buf) < 26) {
strcpy(ret->str,buf);
} no difference.*/
scanf(" %s", &ret->str);
/* str loaded*/
putchar('\n');
puts("First int value: "
scanf(" %d", &ret->p1);
/*first integer */
putchar('\n');
puts("Second, and last int value: "
/* second integer */
scanf(" %d", &ret->p2);
putchar('\n');
return ret;
}
void printall(struct asinine *p) {
/* prints struct members*/
printf("String value:%s ,is given in adjacency with %d , #1 and , %d, #2.\n",p->str,p->p1,p->p2);
}
A sample run looks like:
String value for asinine:
thisnthat
First int value:
12
Second, and last int value:
35
String value:èõÿ¿nthat ,is given in adjacency with 12 , #1 and , 35, #2.
Why the garbled chars?
Thanks.