0

I'm trying to learn C Language using pointers and arrays etc... but my code won't work. Whats is wrong with this code?

#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>

int main(){

    int *number = 0;
    int i = 0;
    int *random = 0;
    int randomTry[100];
    int *getRandomTry = 0;
    int randomGenerator[100];
    int *getRandomGenerator = 0;
    srand (time(NULL));


    *getRandomTry = randomTry[100];
    *getRandomGenerator = randomGenerator[100];

    do{
        *random = rand() % 10;
        getRandomGenerator = random;
        printf("Choosing a number:\n\r");
        for(i = 0; i < 30; i++){
        printf("*");
        Sleep(50);
        }

    printf("\n\r\n\rWell done, your time.");
    printf("\n\r------------------------------");
        printf("\n\rPick a number ( 0 to 10): ");
        scanf("%d", number);
        getRandomTry = number;
        if(*number > 10){
            printf("\n\rRemember, number only from (0 to 10)\n\r");
        }
    else{
        if(number == random){
            printf("\n\rYou choose right!");
            printf("You entered theses numbers till the right answer");
            for(i = 0; i < randomTry[i]; i++){
                printf("%i", getRandomTry);
            }
        }
        else{
            printf("\n\rYou choose wrong, number: %d - random: %d\n\r", number, random);
            Sleep(700);
            system("cls");
        }
    }
    }while(number != random);


    getch();
    return 0;
}

When i compile the program stop working, no warnings or comments show in console.

I'm using the notepad++ with MinGw to compile this code.

6
  • What do you mean 'stops working'? Was it ever working? Do you mean it compiles successfully without error but doesn't do what you want? Have you been able to successfully compile and run a "Hello World" test? If not, be sure to do that so you can verify you understand how to use the development environment first. Commented Mar 28, 2015 at 23:01
  • 1
    Trying to compile this leads to a bunch of warnings for me. Are you sure you enabled warnings? Commented Mar 28, 2015 at 23:05
  • the file: conio.h is specific to windows. I.E. not portable and not a standard library. suggest elimination from the code. then such calls as 'getch()' should be replaced with standard system calls like 'getchar()' Commented Mar 28, 2015 at 23:42
  • the file: windows.h is specific to windows. I.E. not portable and not a standard library, suggest elimination from the code. then use standard C' system function calls. Commented Mar 28, 2015 at 23:44
  • suggest indent the code consistently (and don't use Georgian formatting) Suggest a indent of 4 spaces AFTER an opening brace and unindent 4 spaces before a closing brace. (never use tabs for indentation, with different systems/editors the tab stops in different locations and tab width is not necessarily 8 Commented Mar 28, 2015 at 23:47

2 Answers 2

3

Assigning a value to a NULL pointer is undefined behaviour, such as (in your code).

   int *getRandomTry = 0;
   *getRandomTry = randomTry[100];

gives undefined behaviour on two counts. The second statement tries to retrieve the value of randomTry[100], which doesn't exist (since array indexing starts at zero, and randomTry only has 100 elements). Second, it tries to store that value into a location addressed by the NULL pointer.

With undefined behaviour, anything can happen. Program crashes are pretty common, but not the only possible result.

So you need to understand how array indexing works (it starts at zero, not one). And you need to ensure all pointers point at something valid before trying to set or retrieve values via the pointer.

For example;

 int x = 5;
 int *getRandomTry = &x;
 *getRandomTry = 42;       // will change x
Sign up to request clarification or add additional context in comments.

Comments

-1

The following is a correct declaration of a pointer to an integer, with initial value zero:

int *random = 0;

The following statement will try to place the result of the calculation to the address in memory where the pointer 'random' is pointing:

*random = rand() % 10;

Since 'random' was assigned the value of 0, it is pointing at memory location zero, and the second statement will result in 'undefined behavior', meaning that what happens next in your program is not easily predictable. In practice, it will most likely crash in one way or another, since you cannot write to memory location 0.

if the code was like this:

int random = 0;
...
random = rand() % 10;

then everything would work fine. It would also work if code was like this:

int random_variable;
int *random = 0;
...
random = &random_variable;
...
*random = rand() % 10;

In this last case, 'random_variable' would end up with the result of the calculation.

Hope this helps.

5 Comments

The result of assigning to *random is not an exception, and characterising it that way is misleading. It is undefined behaviour (anything can happen). A common result - but not the only one possible - is an abnormal program termination.
Location zero in all architectures is special in that it always causes an exception, unlike other memory locations especially ones within the process space where 'anything can happen'. However, in principle it is called 'undefined behavior' when such an assignment is made. I'll edit the post accordingly.
There are real-world architectures where assigning to "location zero" is not "special" in the sense you describe.
Also, the null-pointer is only required to compare unequal to any valid pointer. It is not required to be a literal zero. "Address zero" is a red herring, and on modern micro-architectures it is entirely possible to map memory to address zero.
null pointers don't point anywhere (let alone "memory location zero", if the system even has such a thing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.