Thread: code error[return of function]

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Wink code error[return of function]

    hello. my problem is in this code that i write to tes my knowledge of programmer of pointer and allocation dynamic. here go the code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    /****************************************************************************/
    unsigned char *converte_bit(char *bin,int num_pass,int des_bit);
    int informa(int quantia);
    /****************************************************************************/
    int main(){
            unsigned char *p,*bit;         /* variavel num de 0 ate bit_des informado pelo usuario */
            int i,bit_des,num_des;
            printf("Quantos bits deseja entrar: ");
            fscanf(stdin,"%d",bit_des);
            p=(unsigned char *)malloc(bit_des * sizeof(unsigned char));
            fprintf(stdout,"Digite de 1 ate %d: ",informa(bit_des));
            fscanf(stdin,"%d",num_des);
            bit=converte_bit(p,num_des,bit_des);          /* passa o primeiro endereco de *num pra funcao */
            fprintf(stdout,"Numero %d :\n",num_des);
            fprintf(stdout,"%d em binario eh:",num_des);
            for(i=bit_des;i>=0;i--)printf("%d",*(bit+i));
            printf("\n");
            return(0);
    }
    /****************************************************************************/
    unsigned char *converte_bit(char *bin,int num_pass,int des_bit){       /*esta funcao recebe o ponteiro*/
             int i,div=0,decimal=num_pass;
             for(i=0;i<=des_bit;i++){
    /****************************expressoes de conversao*************************/
                    div = decimal % 2;
                    *(bin+i)=div;
                    decimal /= 2;
             }
            return(bin);
    }
    /*************************expressao que devolve a faixa possivel**************/
    int informa(int quantia){
            return(quantia * quantia);
    }
    /***************************fim dos prototipos de funcoes********************/
    /*******************************Por kzinho***********************************/
    the erro is when i execut in my linux this code.
    i execute him and he have been inform me the quantia*quantia...
    he just return the address of one pointer unknow.
    please help me in this situation.

    One kiss in your asses.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    fscanf(stdin,"%d",&bit_des);
    
    ...
    
    fscanf(stdin,"%d",&num_des);
    Forgot the address-of (&) operator for the above fscanf calls.

    Code:
    p=(unsigned char *)malloc(bit_des * sizeof(unsigned char));
    You shouldn't cast the return value of malloc. Also, you don't test if the allocation succeeded (you should). Also, I'm fairly certain that the size of an unsigned char is guaranteed to be 1 so you could just say:

    Code:
    p=malloc(bit_des);
    if( p == NULL )
    {
        // Do something to handle this condition
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Why the heck is this stickied?
    Woop?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by prog-bman
    Why the heck is this stickied?
    That's what I was gonna ask. I thought it very odd. I don't mind... but it just doesn't seem sticky-worthy.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by prog-bman
    Why the heck is this stickied?
    Probably because it represents half the questions asked on this board.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    At least he used code tags
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM