I have a error can somebody help ?

This is a discussion on I have a error can somebody help ? within the C++ Programming forums, part of the General Programming Boards category; I have this code : Code: // Grafuri.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    19

    I have a error can somebody help ?

    I have this code :

    Code:
    // Grafuri.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdlib.h>
    #include <process.h>
    
    #define clrscr() system("cls");
    
    struct sarc;
    
    typedef struct snod{
    	struct snod *urm;
    	struct sarc *a;
    	int cheie;
    } nod;
    
    typedef struct sarc{
    	nod *n;
    	struct sarc *urm;
    }arc;
    
    nod *h_nod=NULL;
    
    //arc *caut_arc(nod *, nod *); // cauta arc
    //arc *caut_arc(int n1, int n2,int *, nod *, nod *); //cauta arc
    nod *caut_nod(nod *, int); // cauta nod 
    nod *add_nod(nod*,int); // adauga un nod;
    arc *add_arc(arc*, int,int); // adauga un arc intre 2 noduri;
    
    nod *caut_nod(nod *lista, int cheie)
    { 
       nod *q1;
    
       for (q1=lista; q1!=NULL && q1->cheie < cheie; q1=q1->urm); 
    
       if (q1!=NULL && q1->cheie == cheie) 
          return q1; 
    
       return NULL;
    } /*caut nod*/
    
    arc *cauta_arc(nod *n1, nod *n2){
    	
    	if (n1==NULL || n2==NULL) return NULL;
    	arc *q1;
    
    	for (q1=n1->a; q1!=NULL && q1->n->cheie < n2->cheie; q1=q1->urm); 
    
    	if (q1!=NULL && q1->n->cheie == n2->cheie) 
    		return q1; 
    	
    	return NULL;
    
    } /*caut arc*/
    
    arc *caut_arc(int n1, int n2 ,nod *nodr1, nod *nodr2 ,int *err=NULL)
    { 
        nod *nod1,*nod2;
    	
    	if ((nod1=caut_nod(h_nod,n1))==NULL) {
    		printf("Nu exista primul nod: %d",n1);
    		if (err!=NULL) *err=1;
    		return NULL;
    	}
    
    	if ((nod2=caut_nod(h_nod,n1))==NULL) {
    		printf("Nu exista al doilea nod: %d",n2);
    		if (err!=NULL) *err=2;
    		return NULL;
    	}
    	
    	if (err!=NULL) *err=0;
    	if (nodr1!=NULL) nodr1=nod1;
    	if (nodr2!=NULL) nodr2=nod2;
    
    	return cauta_arc(nod1,nod2);
    } /*caut_arc*/
    
    nod *add_nod(nod *lista, int v)
    {
       nod *q1, *q2, *aux;
    
       if (caut_nod(lista,v)!=NULL){
    		printf("Nodul %d exista deja\n",v);	
    		return lista;
       }
    
       if ((aux=(nod *)malloc(sizeof(nod)))==NULL) 
       {
          printf("Eroare: memorie insuficienta\n");
          exit(1); 
       }
    
       aux->a=NULL; aux->cheie=v;   
    
       for (q2=q1=lista; q1!=NULL && q1->cheie < v; 
            q2=q1, q1=q1->urm); 
    
       if (q1!=NULL && q1->cheie == v)
       {
          printf("Eroare: Nodul %d apare in tabela\n", v);
          return lista;
       }
    
       if (q1!=q2) /* daca inserarea nu se face la inceputul listei*/
       {
          q2->urm=aux;
          aux->urm=q1;
          return lista ;
       }
    
       /* daca inserarea se face la inceputul listei */
    
       aux->urm=lista;
       return aux;
    }/* add_nod */
    
    int add_arc(int n1, int n2){
    	arc *q1, *q2, *aux;
    	nod *nod1;
    	nod *nod2;
    	nod *l1,*l2;
    	int err;
    	
    	if ((aux=caut_arc(n1,n2,nod1,nod2,&err))!=NULL) {
    		printf("Exista deja un arc intre nodurile: %d,%d",n1,n2);
    		return 3;
    	}
    
    	if (err) return err;
    	/*nod1=caut_nod(h_nod,n1);
    	nod2=caut_nod(h_nod,n2);*/
    
    	if ((aux=(arc *)malloc(sizeof(arc)))==NULL) 
    	{
    		printf("Eroare: memorie insuficienta\n");
    		exit(1); 
    	}
    	aux->n=NULL;  
    
    	for (q2=q1=nod1->a; q1!=NULL && q1->n->cheie < n2; 
    	q2=q1, q1=q1->urm); 
    
    	if (q1!=NULL && q1->n->cheie == n2) // Nu ar trebuii sa se indplineasca aceasta conditie
    	{
    	   //	printf("Eroare: %s apare in tabela\n", s);
    		return 3;
    	}
    
    	if (q1!=q2) /* daca inserarea nu se face la inceputul listei*/
    	{
    		q2->urm=aux;
    		aux->urm=q1;
    		return 0;
    	}
    
    	/* daca inserarea se face la inceputul listei */
    
    	aux->urm=nod1->a;
    	nod1->a=aux;
    	return 0;
    }
    
    void intr(){
    
    	h_nod=add_nod(h_nod,1);
    	h_nod=add_nod(h_nod,3);
    	h_nod=add_nod(h_nod,2);
    	h_nod=add_nod(h_nod,1);
    	h_nod=add_nod(h_nod,5);
    	add_arc(1,3);
    	add_arc(3,5);
    	add_arc(5,1);
    	add_arc(1,3);
    }
    int main(int argc, char* argv[])
    {
    	intr();
    	return 0;
    }
    when I call the function aux=caut_arc(n1,n2,nod1,nod2,&err)) in add_arc I becam the following error :The variable 'nod2' is being used without being defined.

    Can somebody help me ?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    Because you're passing a pointer w/o initializing it.
    This is also C code, not C++.
    You also have errors of passing a pointer to a function that expects a pointer and not the address of a variable. To make things worse, your pointer is unitialized, unallocated, so your program is a ticking time bomb.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    nod2 is not assigned a value, that is correct. Neither is nod1. Since you are passing undefined pointers to the function, the result is undefined. You probably mean to pass the address of the pointer, and have a double pointer in the function .

    Your code is also neither C++ nor C - you are using mostly C, but some of it is C++ - maybe you should decide which you actually want to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    Where is C++? All I see is C. Bundles and bundles of C.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try compiling it as C in gcc for example...

    These bits fall over if you have some level of strictness:
    Code:
    arc *cauta_arc(nod *n1, nod *n2){
    	
    	if (n1==NULL || n2==NULL) return NULL;
    	arc *q1;
    ....
    
    arc *caut_arc(int n1, int n2 ,nod *nodr1, nod *nodr2 ,int *err=NULL)
    { 
        nod *nod1,*nod2;
    ...
    Variable declared after code-statements.
    Default values on arguments

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    I see. So that's now allowed in C, huh?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I see. So that's now allowed in C, huh?
    Not in the currently common implementation of C89. C99 does allow it, but there aren't as many fully C99 compliant compilers out as you may think there would be 8 years later, so it's often best to stick with C89 standard when writing C.

    Of course, at least the "variable declared anywhere" is reasonably common extension [as it's not very hard to backport that code from the C++ compiler that does support that from way before 1999].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21