1
I’m trying to assign 3 names to a string vector within a struct, and using pointers to reference the dynamically allocated struct, and then print and then reverse it into a repeat structure, but when trying to print it out nothing is returned.
String to be printed
printf("%s\n",*(*p).vet[1]); Ana
printf("%s\n",*(*p).vet[2]); Bia
printf("%s\n",*(*p).vet[3]); Lia
String Inverted
printf("%c\n",*(*q).vet[1]); a+n+A
printf("%c\n",*(*q).vet[2]); a+i+B
printf("%c\n",*(*q).vet[3]); a+i+L
Code
#include<stdio.h>
#include<stdlib.h>
typedef struct elemento *ponteiro;
struct elemento
{
int chave;
char vet[100][4];
ponteiro prox;
};
main()
{
ponteiro p,prim,h,q;
int i;
prim=NULL;
p=(ponteiro) malloc(4*sizeof(struct elemento));
h=p;
(*p).chave=1;
*(*p).vet[1]="Ana";
*(*p).vet[2]="Bia";
*(*p).vet[3]="Lia";
printf("%s\n",*(*p).vet[1]);
printf("%s\n",*(*p).vet[2]);
printf("%s\n",*(*p).vet[3]);
for(i=0;i<3;i++)
{
q=(ponteiro)malloc(sizeof(struct elemento));
*(*q).vet[1]=*(*p).vet[1]+*(*p).vet[1,4-i];
*(*q).vet[2]=*(*p).vet[2]+*(*p).vet[2,4-i];
*(*q).vet[3]=*(*p).vet[3]+*(*p).vet[3,4-i];
printf("%c\n",*(*q).vet[1]);
printf("%c\n",*(*q).vet[2]);
printf("%c\n",*(*q).vet[3]);
p=q;
}
}
The problem is mostly in the notation used in pointers and printfs. But I still haven’t understood exactly what the purpose of the exercise is. Is printing several strings backwards ? And it has to be with pointers ?
– Isac
print 3 strings and then invert them using pointers only that
– FZero