0

The following code has a main input of 1-5 which are the amount of times the code will be run. The code prints out the address of the pointers before and after they are assigned.

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

int main(int argc, char **argv)
{
    char character ;
    int integer ;
    char string[50] ;
    float fp ;
    int i, x = 5 ;
    
    char* cPointer ;
    int* intPointer ;
    char* sPointer ;
    float* fpPointer ;
    
    if (argc > 1)
    {
        x = atoi(argv[1]) ;
    }
    else if (x > 5 || x < 1)
    {
        printf("Invalid number, please input a number between 1-5!") ;
        exit (1) ;
    }
    
    int count = 0;
    while (count <= x)
    {
        printf("Please enter a character: ") ;
        scanf(" %c", &character) ;
        printf("Please enter an integer: ") ;
        scanf("%d", &integer) ;
        printf("Please enter a string: ") ;
        scanf("%s", string) ;
        printf("Please enter a floating point:") ;
        scanf("%f", &fp) ;
        
        printf("\n\nCharacter Pointer: %p", cPointer) ;
        printf("\nInteger Pointer: %p", intPointer) ;
        printf("\nString Pointer: %p", sPointer) ;
        printf("\nFloating Point Pointer: %p", fpPointer) ;
        
        printf("\n\nCharacter: %c", character) ;
        printf("\nInteger: %d", integer) ;
        printf("\nString: %s", string) ;
        printf("\nFloating Point: %.2f", fp) ;
        
        cPointer = &character ;
        intPointer = &integer ;
        fpPointer = &fp ;
        sPointer = string ;

        printf("\n\nCharacter Pointer: %p", cPointer) ;
        printf("\nInteger Pointer: %p", intPointer) ;
        printf("\nString Pointer: %p", sPointer) ;
        printf("\nFloating Point Pointer: %p\n\n", fpPointer) ;
        
        count++ ;
    }

    return 0;
}

The problem comes with the output, where instead of the pointer address for the floating point I am getting (nil) for the first try

Character Pointer: 0x7f0eeed0c1f8                                                
Integer Pointer: 0x7f0eef2d6000                                                  
String Pointer: 0x7f0eef2e91c8                                                   
Floating Point Pointer: (nil)                                                    
                                                                                 
Character: a                                                                     
Integer: 1                                                                       
String: aa                                                                       
Floating Point: 1.10                                                             
                                                                                 
Character Pointer: 0x7ffe236e563f                                                
Integer Pointer: 0x7ffe236e5640                                                  
String Pointer: 0x7ffe236e5670                                                   
Floating Point Pointer: 0x7ffe236e5644                                           
                                                                                 
Please enter a character: b                                                      
Please enter an integer: 2                                                       
Please enter a string: bb                                                        
Please enter a floating point:2.2                                                
                                                                                 
                                                                                 
Character Pointer: 0x7ffe236e563f                                                
Integer Pointer: 0x7ffe236e5640                                                  
String Pointer: 0x7ffe236e5670                                                   
Floating Point Pointer: 0x7ffe236e5644                                           
                                                                                 
Character: b                                                                     
Integer: 2                                                                       
String: bb                                                                       
Floating Point: 2.20                                                             
                                                                                 
Character Pointer: 0x7ffe236e563f                                                
Integer Pointer: 0x7ffe236e5640                                                  
String Pointer: 0x7ffe236e5670                                                   
Floating Point Pointer: 0x7ffe236e5644  
  • 2
    What happens if you print `*cPointer`, and similarly for the other `Pointer`s? (Hint: You didn't initialize *any* of the `Pointer` variables.) – Scott Hunter Apr 11 '21 at 21:27
  • @ScottHunter I'm not exactly sure what you mean... However, the variables are given a value through scanf and the pointers do have an address before and after. I am only missing the floating point for some weird reason. –  Apr 11 '21 at 21:32
  • 2
    @Jon The pointers have an uninitialized value at first, so the values you print in the first run are meaningless. – Emanuel P Apr 11 '21 at 21:36
  • You actually invoke *UB* in `printf()`. See this : https://stackoverflow.com/questions/24867814/printfp-and-casting-to-void – alex01011 Apr 11 '21 at 22:46

1 Answers1

1

You are not initializing the pointers you declare:

char* cPointer ;
int* intPointer ;
char* sPointer ;
float* fpPointer ;

Then you are trying to print their values:

printf("\n\nCharacter Pointer: %p", cPointer) ;
printf("\nInteger Pointer: %p", intPointer) ;
printf("\nString Pointer: %p", sPointer) ;
printf("\nFloating Point Pointer: %p", fpPointer) ;

The content of those variables is undefined! Reading an uninitialized variable is undefined behavior. Read more here: What happens to a declared, uninitialized variable in C? Does it have a value? You cannot expect any meaningful result, so if some of them prints out as (nil) it's just because for some reason they happened to be initialized to 0. The values you print here are whatever gibberish is on the stack at the time of printing, they are meaningless. For what it's worth, trying to print them might as well crash the program.

Then, before the second group of printfs you actually assign some values to the pointers:

cPointer = &character ;
intPointer = &integer ;
fpPointer = &fp ;
sPointer = string ;

Of course, now they are all properly initialized to a value, and you will get meaningful results when printing.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Here are the specific instructions I received "Define pointers to those variables types without any initialization of those points to the previous variables. " How would u have me fix the problem? –  Apr 11 '21 at 21:46
  • 1
    @Jon if your instructions were *"declare a bunch of pointers, don't initialize them and then print their value"* I'd suggest writing an e-mail to your professor asking them from which cereal box they got their degree from, I want one too. To fix the problem, well, just don't print variables before initializing them. That's it. – Marco Bonelli Apr 11 '21 at 22:07