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 asstruct nodo *
. If not, your list does not include the pointer to the next element.– Isac
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.
– Wesley Ferreira
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
andelo_a
as pointers, making sense of the assignments incria_pnd
, or else make it whole from beginning to end and completely change the assignments within theswitch
within the functioninserir
– Jefferson Quesado