Address type Struct for INT

Asked

Viewed 325 times

1

I have the following Struct:

struct nodo{
int elo_a;
char nick[26];
char cidade[16];
int idade;
int elo_p;};

I have both struct assignments:

struct nodo *variavel;
struct nodo *offline; 

Ok, I already entered a value offline, setando int elo_p and int elo_a as NULL, since it is the first element in a doubly chained list. Now I enter values in the variable struct and want to assign the memory address of the same to the int elo_p from my first offline struct log, I did it as follows offline->elo_p = variavel; but a mistake comes up saying: invalid Conversion from 'nodo*' to int. I know I could solve the problem by changing the variable int elo_p for struct nodo *elo_p but I need to store the memory address inside the int elo_p, is there any way? Or am I doing something very wrong? Full Code:

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

#define TAM 30

struct nodo{
    int elo_a;
    char nick[26];
    char cidade[16];
    int idade;
    int elo_p;
};
struct nodo *variavel; // troquei variavel estatica por dinamica
struct nodo  l_dupla[TAM]; //pnd

int top = TAM - 1;

void cria_pnd(void){ // codigo disponivel no trabalho *pnd*
     int i = 0;
     while(i < TAM-1){
           l_dupla[i].elo_p = ++ i;
     }
     l_dupla[TAM-1].elo_p = -1;
}

int pilha_pop(void){// função tirar da pnd
    if(top >= 0)
    return (-- top);

}

struct nodo pilha_push(void){// função inserir na pnd
    if(top>=0){
        return  l_dupla[top];
        top++;
    }else
        printf("Vazio ou nao existe nodo\n");

}

struct nodo retirada(void){
        struct nodo n;
        n = pilha_push();
        return n;
}

void inserir(struct nodo *offline, struct nodo *online,struct nodo *ignorados){
    int sw,indice;
    indice = pilha_pop();

    variavel = (struct nodo*) malloc(sizeof(struct nodo));

    printf("Escolha em qual lista deve ser incluido o usuario \n 1 - offline  2 - online  3 - ignorados \n" );
    scanf("%d",&sw);
    fflush(stdin);

    printf("Digite os valores para o usuario \n");
    printf("nick:\n");
    fflush(stdin);
    scanf("&s",variavel->nick);
    fflush(stdin);
    printf("cidade:\n");
    scanf("%s",variavel->cidade);
    fflush(stdin);
    printf("idade: \n");
    scanf("%d",variavel->idade);

    //inserir controle dos elos
    //inserir
    switch (sw){
        case 1:
            offline = (struct nodo*) malloc(sizeof(struct nodo));
            if(offline == NULL){
                offline = variavel;
                offline->elo_a = NULL;
                offline->elo_p = NULL;
            }else{
                offline->elo_p = variavel;
                variavel->elo_a = offline;

                offline = variavel;
                offline->elo_a = variavel->elo_a;
                offline->elo_p = NULL;
            }

            break;
        /*case 2:
        online = (struct nodo*) malloc(sizeof(struct nodo));
            online = &variavel;
          break;
        case 3:
            ignorados = (struct nodo*) malloc(sizeof(struct nodo));
            ignorados = &variavel;
            break;*/
        default:
            break;
   }
}
/*void mostar(void){
    int escolha = 0;
    printf("Digite a lista desejada: 1 - offline  2 - online  3 - ignorados:");
    scanf("%d",&escolha);

    switch (sw){
        case 1:

            break;
        case 2:
            online = &variavel;
          break;
        case 3:
            ignorados = &variavel;
            break;
        default:
            break;
   }
}*/

int main()
{
    struct nodo *offline = NULL;
    struct nodo *online = NULL;
    struct nodo *ignorados = NULL;
    char ch;
    cria_pnd();

    do{
        printf(" _____________________________________\n|\t\tmenu \t\t      |\n|_____________________________________|\n");
        printf("| I- incluir R- Retirada M - Mostar   |\n| T - troca de status D - Nodos disp  |\n|\t        S- Sair\t\t      |\n");
        printf("|_____________________________________|\n ");
        fflush(stdin); //PRECISA DE 2 FFLUSH
        scanf("%c",&ch);
        fflush(stdin); //PRECISA DE 2 FFLUSH

        switch (ch){
            case ('I'):
                inserir(offline,online,ignorados);
                break;
            case 'R':
               //variavel  =retirada();
                break;
            case 'M':
                //mostrar();
                break;
            case 'D':
                printf("Nodos disponiveis:%d",top+1);
            default:
                break;
        }
    }while(ch!='S');
    return 0;
}
  • But the elo_p was supposed to be the pointer to the next item on the list? If so, it must be declared as struct nodo *. If not, your list does not include the pointer to the next element.

  • I don’t understand why you want to save the address of the variable. Can you post the full code? Only with this it is difficult to help.

  • Your code doesn’t make sense. Pointers on 64-bit machines cannot store in their completeness within integers, so you are already losing the real address here. Do as Isac pointed out and treat variables elo_p and elo_a as pointers, making sense of the assignments in cria_pnd, or else make it whole from beginning to end and completely change the assignments within the switch within the function inserir

1 answer

1


The problem

The biggest problem of keeping one ponteiro within a inteiro is that a ponteiro may be greater than a inteiro, so that you may not be able to access the memory region you want, since you have lost some bytes of information when a ponteiro into a inteiro.

If you really need to save whole and addresses in the same variable, you can use a long long in place of a int and compile using the flag -fpermissive, but still it will not be guaranteed that you will have a code that works for any architecture.


Example of implementation

Below I’ll show you an example of code using this strategy you’re looking for, but I think it’s best to think of a safer way to implement this code.

#include <stdio.h>
#include <stdlib.h>

struct nodo{
    long long elo_a;
    char nick[26];
    char cidade[16];
    int idade;
    long long elo_p;
};

struct nodo *variavel;
struct nodo *offline;

int main() {
    // Alocando memória para offline
    offline = (struct nodo *) malloc(sizeof(struct nodo));

    // Alocando memória para variavel
    variavel = (struct nodo *) malloc(sizeof(struct nodo));

    // Adicionando um valor a idade de variavel
    variavel->idade = 23;

    // Colocando o endereço de variavel para offline->elo_p
    offline->elo_p = variavel;

    // Acessando o endereço de variavel
    struct nodo *aux = offline->elo_p;

    // 23
    printf("%d\n", aux->idade);

    return 0;
}

Compile as follows

gcc exemplo.cpp -fpermissive
  • Even so, the implementations of cri_pnd and inserir contradict the typing. If it were more coherent/homogeneous, it would be better...

  • In fact using a pointer type struct is more reliable, wanted to know the implications in using an int, now it makes more sense

  • Use a long long to save a pointer for another element is not at all good practice, especially when it has no advantage in this case, and will still make it difficult to debug and errors in the future.. You should at least put in your answer that it is not a reasonable solution, and say how it should be done.

Browser other questions tagged

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