-4
#include <stdio.h>
#include <stdlib.h>
#include <array.h>

int col, str;
int *point;

void setArr()
{
    printf("Input columns="); scanf("%d", &col);
    printf("Input strings="); scanf("%d", &str);
    int num[str][col];
    for(int i = 0; i < str; ++i)
    {
        for(int j = 0; j < col; ++j)
        {
            scanf("%d", &num[i][j];
        }
    }
    point = num;
}

int main(void)
{
    setArr();
    printf("First=%d\n", *point);
    printf("Number=%d", *point);
}

Output:

Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104

Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
Wage
  • 33
  • 4
  • 5
    please post code and output as text, not as image. – Stephan Lechner Nov 07 '18 at 22:50
  • No, here we don't have code. We have an image of your screen. Code is text (unless you're writing yours in an image editor), and that's how it needs to be posted here. – Ken White Nov 07 '18 at 22:52
  • && everytime you use global variables a kitty dies :( – Swordfish Nov 07 '18 at 22:52
  • Possible duplicate of [How to return local array in C++?](https://stackoverflow.com/questions/7769998/how-to-return-local-array-in-c) – The Dark Nov 07 '18 at 22:52
  • 2
    Does the compiler warn you that you assign a 2D array to pointer to `int`? Whatever, you'll get the array's first element. I can't check the compiler warnings because I can't compile the code because an image of code can't be compiled. – Weather Vane Nov 07 '18 at 22:55
  • @The Dark: dup shows how to allocate a 1D array; might be more tricky in case of a 2d array. – Stephan Lechner Nov 07 '18 at 22:56
  • Ah, now I get *warning C4047: '=': 'int *' differs in levels of indirection from 'int (*)[10]'* – Weather Vane Nov 07 '18 at 23:01
  • @WeatherVane You are telling fairytales. C4047 is a warning of the Microsoft C/C++-compiler. But the Microsoft C/C++-compiler doesn't support variable-length arrays in the first place. (and doesn't know of ``) – Swordfish Nov 07 '18 at 23:04
  • @Swordfish you are right, I hard coded the array definition as `int num[10][10];` but the same applies. – Weather Vane Nov 07 '18 at 23:05
  • See my answer. First thing is that you have your `point` refer to an address of a function-local variable, 2nd is that the types don't match. – Swordfish Nov 07 '18 at 23:06
  • how was i rude?? – Swordfish Nov 07 '18 at 23:09
  • The code you have shown us will generate lots of compiler warnings and cannot possibly give the output you show. Show us real code and real output. – Lee Daniel Crocker Nov 07 '18 at 23:32

1 Answers1

2

With

point = num;

you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.

Swordfish
  • 12,971
  • 3
  • 21
  • 43