0

I finished three programs today, I am a student and I tend to overthink I can do more than I did. The problem is when I input 3 numbers, shouldn't there be three memory addresses? I feel like my program is missing a component that moves the pointer. The code is provided below. Why in our career would memory address be used?

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


void options()
{
    puts("\t\t\tMain Menu");
    puts("\t\ta. Enter New Integer Value");
    puts("\t\tb. Print Pointer Address");
    puts("\t\tc. Print Integer Address");
    puts("\t\td. Print Integer Value");
    puts("\t\te. Exit");
    puts(" \tPlease enter an option from the menu: "); 
}//end options
    

int main(void) {
    //Intialize and Declare variables
    char menuOption;
    char valueStr [50];
    int i; 
    int *iPtr= NULL;

    while(menuOption != 'e')
    {
    //Four Menu Options
    options();
    menuOption = getc(stdin);
    getc(stdin);
    

    switch(menuOption){

    case 'a':
    //Enter New Integer Value 
    printf("Enter an Integer: ");
    gets(valueStr);
    i = atoi(valueStr);
        break;
    case 'b':
        //Print Pointer Address
    iPtr = &i;
    printf("Address of Pointer is %p\n", (void *) iPtr);
        break;
    case'c':
    //Print Integer Address 
    printf("Address of integer is %p\n", (void *) &i);
        break;
    case'd':
    //Print Integer Value 
    printf("The integer value is %d\n", i);
    break;
    case'e':
        break;
    default:
    printf("Invalid Option, try again\n");
    break;
    }//end Switch Statement
    }//end while statement
    return 0;

}
JLpng
  • 11
  • 1
  • 4
  • 2
    You are invoking *undefined behavior* by using value of uninitialied non-static local variable `menuOption`, which is indeterminate. – MikeCAT Jun 11 '21 at 23:34
  • 2
    You have only one variable to store the integers read, so there will be only one address for storing integers read. – MikeCAT Jun 11 '21 at 23:37
  • 2
    By the way, [never ever use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Oh, and indenting your code properly would make it a lot nicer for you and everyone else to read and understand. – Nate Eldredge Jun 11 '21 at 23:54
  • @MikeCAT Thank you for your feedback. I am a beginner, so I know a lot of programs I've made are going to have a lot of faults. They're one-dimensional, I'll be the first to admit I am at the stage of being aware I am incompetent at programming. When you say undefined behavior, do you mean when this program is ran a certain amount of times it is unpredictable? Do you have any articles or lectures that would help me understand ways to prevent that in programs I create in the future? – JLpng Jun 13 '21 at 00:57
  • @NateEldredge I heard gets() is a big no-no in real world C programming. I've been learning C for the fundamentals of programming. I wanted to play around with different functions while I am in a controlled environment. Thank you for the additional read. I used fgets() in a previous project. – JLpng Jun 13 '21 at 00:58
  • @JLHarris: Regarding undefined behavior, see https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior. For a beginner, translate the phrase "undefined behavior" as "this could cause your program to do bad things, so don't do it". – Nate Eldredge Jun 13 '21 at 01:04

1 Answers1

1

The variable i is always at the same location in memory throughout the execution of main, so no matter how many times you print its address, you'll see the same value. That's an important property of the way pointers work in C: any given variable keeps the same address throughout its lifetime. If this were not true, then pointers would be impossible to use reliably, because even if the pointer stayed the same, you'd have no way of knowing if the variable had moved out from under it.

When the loop iterates for a second time, assigning to i overwrites the previous value at that location with the new value. It doesn't allocate a separate location for it, if that's what you were thinking. Besides violating the above rule, that would also mean that any significant loop would run you out of memory almost immediately.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • 1
    Another important concept here is that *objects*, such as local variables, have storage locations that (in most cases) can be identified by addresses, but *values* do not have addresses independent of the object, if any, in which they are stored. Thus no, the OP's apparent idea that multiple values entered implies multiple addresses is not well founded. It is essential to distinguish between objects' identities and their stored values. – John Bollinger Jun 12 '21 at 00:08
  • Thank you this is an excellent explanation and confirms the information I was reading on pointers and memory addresses. – JLpng Jun 13 '21 at 00:51