4

When I compile this code it says "error C4700: uninitialized local variable 'b' used". I'm not sure what I have to do now to fix this problem. I'm neither an IT student or technican but I very like to learn C++ and I'm learning it by myself. I've been on this for 1 day.

Many thanks

#include <stdio.h>
#include <iostream>

//A. 
//1--
void InputArray(int *a, int &n)
{
    printf("Insert n = ");
    scanf("%d", &n);
    a = new int[n];
    for (int i=0; i<n; i++)
    {
        printf("Enter the key's a[%d] values: ", i);
        scanf("%d",&a[i]);
    }
}


void main()
{
    int *b, m;
    InputArray(b, m);
}
bemeyer
  • 6,154
  • 4
  • 36
  • 86
Ben
  • 215
  • 1
  • 2
  • 8
  • Think about the value that `b` has when you pass it to your function. –  Feb 20 '14 at 19:20
  • Possible duplicate of [How do I best silence a warning about unused variables?](http://stackoverflow.com/questions/1486904/how-do-i-best-silence-a-warning-about-unused-variables) – rocambille Jan 02 '17 at 18:06

4 Answers4

16

b is passed by value, which means a copy will be made, but since it's not initialized, you get the warning. Simply initialize it:

int *b = nullptr;

or

int *b = NULL;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

If you want the function to modify the caller's variable, then pass by reference:

void InputArray(int *&a, int &n)
                     ^

Your version passes the uninitialised pointer by value; the function modifies a local copy of it, but leaves b in its uninitialised state.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Possibly true, but not an answer to the problem. –  Feb 20 '14 at 19:20
  • @MikeW: In what way doesn't it answer the question? The function is clearly intended to allocate an array, and return the base pointer and size via reference parameters. The warning tells you that that's not happening, since the pointer is passed by value instead, leaving the caller's variable unchanged. – Mike Seymour Feb 20 '14 at 19:22
  • Hi, both of the ways lead to exactly what I need in the Command Prompt. However, I don't really understand the difference of this one: (1) void InputArray(int*& a, int& n) which does not require to initialize the value of b to be NULL >>>>>>>> and this one (2) keep the function void InputArray(int* a, int& n), then initialize b afterwards>>>>>>>>>>> But anyway, I'm very grateful for your help. Thank you both – Ben Feb 21 '14 at 03:28
1

The pointers are not default initialized, so your variable b is uninitialized, this is the source of error. You have to initialize this variable to fix this:

void main()
{
    int *b = NULL, m;
    InputArray(b, m);
}

After you fix this there is additional problem in your code. It seems from the way you call a function that you expect to persistently change pointer b passed into it, so that b will point into memory allocated with new after function returned. But you pass a pointer by value what means changes made in function will not be reflected in original variable b which will still point to what it pointed before the call to a function. (the array will be allocated inside function and will stay in memory after function returned but you will leak this memory as b won't point into it). To fix this you have to pass pointer by reference:

void InputArray(int*& a, int& n)

Also: where is delete? Remember: mapping new to delete is bijection: every new corresponds to single delete placed somewhere in code.

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Hi @piotrus, thank you for your answer but what is the difference between (1) void InputArray(int*& a, int& n) which does not require to initialize the value of b to be NULL and (2) keep the function void InputArray(int* a, int& n), then initialize b afterwards. Sorry for asking this kind of newbie question but the pointer for me it's too difficult to understand. – Ben Feb 21 '14 at 03:22
  • @user3247523 what you mean by initialize b afterwords? the & is needed to take pointer by reference in order to change original pointer passed to function not a local copy of this pointer, [here](http://www.codeproject.com/Articles/4894/Pointer-to-Pointer-and-Reference-to-Pointer) you will find this explained with examples – 4pie0 Feb 21 '14 at 11:06
-3

First of all, did you learn how to use an pointer correctly ? because if you know how to use pointer u should know that when you declare a pointer you need to be initialized to NULL before you can use it, correct me if i'm wrong.

Example

int *b = nullptr;
int *b = NULL;
int *b = 0;
int *b(0);

It's all the same thing but in an different way

  • 1
    You don't need to initialize a pointer to a null value : you need to initialize it with a value which may or may not be null, and you can't use a null pointer in the sense you can't dereference it. Moreover, `NULL` is an alias for `0` on most compilers. On the other hand, `nullptr` is a true pointer type and is different from `NULL` and `0` – rocambille Jan 02 '17 at 17:59