Answers for "simple example of pointers"

C
0

simple example of pointers

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

int main() {
    int *nums;
    int x;
//	Allocate storage for 3 integers 
    nums = (int *)malloc( sizeof(int) * 3 );
    if( nums==NULL ) {
        fprintf(stderr,"Memory allocation error\n");
        exit(1);
    }
//	Assign the integer values
    for( x=0; x<3; x++ )
        *(nums+x) = x+1;
//	Output the results
    printf("%d %d %d\n",
            *(nums+0),
            *(nums+1),
            *(nums+2)
          );
    return(0);
}
Posted by: Guest on June-06-2021

Code answers related to "C"

Browse Popular Code Answers by Language