Array of pointers with struct

Asked

Viewed 471 times

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 ?

  • print 3 strings and then invert them using pointers only that

1 answer

2


The pointer notation used is not correct in some places according to the declared type. When it does:

*(*p).vet[1]="Ana";

You already have a lot of problems, because vet is an array of strings, so vet[1] is a string, so it has a * more at the beginning. However attribution of strings in C is done with strcpy and the (*p). can be simplified to p->. Using all that I mentioned this instruction would be:

strcpy(p->vet[1], "Ana");

When assigning the pointer q also has the same problem, and the statement of strings vet[100][4] It certainly turned out the other way around 100 size strings 4.

Ended up complicating quite a lot in the solution even with the restriction of using pointers. If the goal is to invert an array of 3 strings using pointers, then only this is enough:

#include <stdio.h>
#include <string.h>

//função de inversão utilizando ponteiros
void inverter(char *str){
    int tam = strlen(str), i;
    for (i = 0; i < tam / 2; ++i){
        char temp = *(str + i);
        *(str + i) = *(str + tam - i - 1);
        *(str + tam - i - 1) = temp;
    }
}

int main(){
    char strings[4][100];
    strcpy(strings[0], "Ana");
    strcpy(strings[1], "Bia");
    strcpy(strings[2], "Lia");

    int i;
    for (i = 0; i < 3; ++i){
        inverter(strings[i]);
        printf("%s\n", strings[i]);
    }
    return 0;
}

Watch it work on Ideone

  • strcpy(p->vet[1], "Ana"); helped me in the first part but the algorithm has to be the same

Browser other questions tagged

You are not signed in. Login or sign up in order to post.