0

--------------------WORKS------------------------------------

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

int main()
{
    int n, *p, i;

    printf("HOW MANY NUMBERS: ");
    scanf("%d", &n);

    if (p == NULL)
    {
        printf("MEMORY ALLOCATION UNSUCCESSFUL");
        return 1;
    }

    for (int i = 0; i < n; i++)
    {
        printf("\nENTER NUMBER %d: ", i + 1);
        scanf("%d", p + i);
    }

    printf("\nTHE  NUMBERS ARE:\n");
    for (i = 0; i < n; i++)
        printf("%d ", *(p + i));

    free(p);
    return 0;
}

---------------------------DOESNOT WORK------------------------------------------

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

int main()
{
    int n, *p, i;

    printf("HOW MANY NUMBERS: ");
    scanf("%d", &n);

    if (p == NULL)
    {
        printf("MEMORY ALLOCATION UNSUCCESSFUL");
        return 1;
    }
//i did 'i' instead of 'int i'
    for ( i = 0; i < n; i++)
    {
        printf("\nENTER NUMBER %d: ", i + 1);
        scanf("%d", p + i);
    }

    printf("\nTHE  NUMBERS ARE:\n");
    for (i = 0; i < n; i++)
        printf("%d ", *(p + i));

    free(p);
    return 0;
}

when i removed the int from for loop the memory doesnot seems to be allocated and i don't seem to understand why? but when i do block scope declaration i.e int = 0 in loop the code works fine

  • 3
    Neither version of the code allocates any memory for `p` to point at. It's accident that either appears to work — that's the beauty of undefined behaviour. – Jonathan Leffler Jul 31 '23 at 16:32
  • 2
    How is `p` assigned *anything* here? It's never initialized, it's undefined behaviour. `int *p = NULL`. Don't stack variable definitions. In general you should *always* initialize to some known-good value. – tadman Jul 31 '23 at 16:32
  • but it works and i don't know why? – HelptimeCode Jul 31 '23 at 16:34
  • There's no reason why — the behaviour of the code is undefined. See [Undefined, unspecified and implementation-defined behaviour](https://stackoverflow.com/q/2397984/15168) – Jonathan Leffler Jul 31 '23 at 16:37
  • You got "lucky" that it worked, since in both cases `p` is uninitialized. On a related note, it seems you're missing a call to `malloc`. – dbush Jul 31 '23 at 16:37
  • please sir suggest me what can i do , i was just following a code from my textbook and got curious and asked question – HelptimeCode Jul 31 '23 at 16:38
  • "but it works and i don't know why?". With undefined behavior any thing can happen, sometimes it seems to work, sometimes it crashes, sometimes ... . One thing sure: that does not work! – dalfaB Jul 31 '23 at 16:40
  • If your textbook is missing `p = malloc(n * sizeof(*p));` or something similar (before the `if (p == NULL)` test), chuck the book away. The code doesn't verify that the `scanf()` operations succeed either — that's barely excusable in the early stages of teach C, but is ominous too. By the time you're dealing with dynamic memory allocation, you should be able to deal with error checking too. – Jonathan Leffler Jul 31 '23 at 16:40
  • [here is my book reference sir](https://ibb.co/jhNtkzc) – HelptimeCode Jul 31 '23 at 16:45
  • Style suggestion: `*(p + i)` is rarely the best choice of notation. Use `p[i]` instead. It is clearer, and even without spaces, it is shorter, too. – Jonathan Leffler Jul 31 '23 at 16:45
  • 4
    Given what the image shows, throw the book away. Better: burn it or mutilate it so that it cannot be read any more; then no one else can be misled by it. The layout of the code is horribly inconsistent too — another reason to forcibly discard the book. Consistency is important for readability, and a good, consistent layout makes it easier to read the code. It took me some moments to spot the close brace over in column 80 (or thereabouts). – Jonathan Leffler Jul 31 '23 at 16:46
  • The guy who wrote this book should be permanently banned from any computer activities. It is something definitely too difficult for him. – 0___________ Jul 31 '23 at 16:59
  • As a side note, you don't even have to store all 25 numbers to find the largest one. Just read one and see if it is larger than the one you already have, then save it, otherwise throw it away. – BoP Jul 31 '23 at 18:50

1 Answers1

3

Your program invokes undefined behaviour from its almost first line.

    if (p == NULL)

p is not initialized and it has indeterminate value. This does not make any sense as defining a pointer does not allocate any memory (you need to do it yourself)

    printf("HOW MANY NUMBERS: ");
    if(scanf("%d", &n) != 1) return 1;

    p = malloc(n * sizeof(*p));
    if (p == NULL)

scanf("%d", p + i); you use not initialized pointer - another UB

The rest of the code does not matter as you have already invoked undefined behaviour.

After the correction, both versions will work correctly.

0___________
  • 60,014
  • 4
  • 34
  • 74