c - Pointers and the Memory Pool: Simple Test Program -


i trying teach myself how create, mutate pass pointers , using blocks of memory memory pool. attempting due in program below return pointer block of memory memory pool (that malloced), giving me error. if point me in right direction explaining error, showing me how fix (or lead me in right direction) fantastic!

here code have right (and further below error message):

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <time.h>  int enemylife(int level) {     int randtime;     int *ptr;     srand(time(null));     ptr = (int*) malloc(sizeof(int)*level);      (int = 0; < level; ++i)     {         randtime = rand() % 100;         *ptr++ = randtime;     }      return *ptr; };  int main(void) {     int ammount, randvalue;     int (*ptrenemylife) (int) = enemylife;      printf("ammount of random number printed screen?\n");      scanf("%d", &ammount);      int *ptr;      *ptr = (*ptrenemylife) (ammount);     printf("%d\n", *ptr);      return 0; } 

... , error after peer advised me compile using -wall flag.

/home/definity/desktop/cloud/code/rand.c: in function ‘main’: /home/definity/desktop/cloud/code/rand.c:39:7: warning: ‘ptr’ used uninitialized in function [-wuninitialized] /home/definity/desktop/cloud/code/rand: in function `enemylife': /home/definity/desktop/cloud/code/rand.c:8: multiple definition of `enemylife' /tmp/ccvzvkya.o:/home/definity/desktop/cloud/code/rand.c:8: first defined here /home/definity/desktop/cloud/code/rand: in function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here /home/definity/desktop/cloud/code/rand: in function `data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here /home/definity/desktop/cloud/code/rand: in function `data_start': (.data+0x8): multiple definition of `__dso_handle' /usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here /home/definity/desktop/cloud/code/rand:(.rodata+0x0): multiple definition of `_io_stdin_used' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here /home/definity/desktop/cloud/code/rand: in function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here /home/definity/desktop/cloud/code/rand: in function `main': /home/definity/desktop/cloud/code/rand.c:28: multiple definition of `main' /tmp/ccvzvkya.o:/home/definity/desktop/cloud/code/rand.c:28: first defined here /home/definity/desktop/cloud/code/rand: in function `_init': (.init+0x0): multiple definition of `_init' /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here /usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__tmc_end__' /home/definity/desktop/cloud/code/rand:(.data+0x10): first defined here /usr/bin/ld: error in /home/definity/desktop/cloud/code/rand(.eh_frame); no .eh_frame_hdr table created. collect2: error: ld returned 1 exit status [finished in 0.1s exit code 1] 

there quite few errors here. have added comments within snippet of code explain doing @ each step, compare original code , see why did did. following think attempting (attempting follow logic):

#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <time.h>  int *enemylife( int const level ) {   // requests block of memory of size int * level    // memory pool.   int *ptr = malloc( sizeof( int ) * level );    // determines if ran out of memory memory pool. important    // check result system call.   if ( ptr == null ) {     exit( exit_failure );   }    srand( time( null ) );    // iterates level times , stores random number in pointer ptr    // @ ith position.   ( int = 0; < level; i++ ) {     ptr[ ] = ( rand() % 100 ); // side: ptr[ ] => *( ptr + )   }    // returning pointer integer.   return ptr; }  int main( void ) {   int amount;    printf( "amount of random numbers printed screen?\n" );   scanf( "%d", &amount );    // defining pointer integer ptr , storing result    // enemylife in pointer. passing "amount" because want many    // numbers.   int *ptr = enemylife( amount );    printf( "outputting random values.\n" );    // iterate on every position in returned pointer each random    // number. output stdout.   ( int = 0; < amount; i++ ) {     printf( "%d\n", ptr[ ] );   }    // free memory block ptr points to. no longer need it.   free( ptr );    return 0; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -