-2

How can I create an array of pointer using **P and point it to NULL?

Let's say I have coded as below .

struct s
{
    float  a ;
    char x ;
} ;

s  **p ;

p = new ( s  * [10] ) ;

Now I want to make some of them as NULL

p[0] = NULL ;

p[5] = NULL ;

If I simply coded as above then it gives me a warning and also the above method is wrong in how it makes the variable NULL. So how to do I do this?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
john
  • 337
  • 2
  • 15

4 Answers4

0

If you need that elements of the array were zero initialized you can write

p = new s * [10] {};
timrau
  • 22,578
  • 4
  • 51
  • 64
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I don't need all element to make zero or NULL i need only some of them and also it gives compilation error on my pc . – john Nov 13 '13 at 17:28
0

It should be

p = new s*[10];
p[0] = NULL;
p[5] = NULL;

If the error is something like

error: ‘NULL’ was not declared in this scope

then, either

#include <cstdlib>

in the beginning, or

p[0] = 0;
p[5] = 0;
timrau
  • 22,578
  • 4
  • 51
  • 64
  • Both are same either we can code it as p = new s *[10] ; or p=new ( s * [10] ) ; – john Nov 13 '13 at 17:24
  • What's the warning, then? – timrau Nov 13 '13 at 17:25
  • @ timaru There is no error message . This gives a warning message and doing this is illegal , illegal in the sense if we coded as above than it can not be made NULL . There may be cause that pointer to pointer can not be made as NULL . – john Nov 15 '13 at 08:03
0

I've coded this

#include<cstdlib> //NULL definition

struct s{
  float a;
  char c;
};

int main (int argc, char **argv){
  s **p = new s*[10];
  p[0] = NULL;
  p[5] = NULL;
  return 0;
};

in test.cpp and complied it with g++ ./test.cpp and got no warning or error.

So please provide the whole code you are trying to compile and the compiler output. Otherwise it is hard to help you.

JustAnotherCurious
  • 2,208
  • 15
  • 32
  • @sonu I got the code you give me and there is a problem when you try to pass the result of deque to inque because you have decalared the inque to get reference to pointer and you can make a reference only from lvalue, but deque returns rvalue, so change inque argument type from dary *& to dary *. But you code is hard to read and I can't understand how your question is connected to the code you provide in the comment above. – JustAnotherCurious Nov 13 '13 at 18:31
  • OK forget about that statement and check statement of crate function . Where I have created the point cptr using new and I made all of them as NULL using for loop . In for loop it is giving no effect to make it NULL . – john Nov 14 '13 at 04:30
  • @ just http://textuploader.com/d3uk try this uploaded new version with modified inque argument . – john Nov 14 '13 at 04:40
0

This should give you an array of type S pointer with length 10, all containing null. I would get into the habbit of using nullptr. NULL is 0 of type int where nullptr is 0 of type void* (void pointer)

struct S
{
    float  a ;
    char x ;
};

int main()
{
    S** p = new S*[10];

    for(int i = 0; i < 10; i++)
    {
        p[i] = nullptr;
    }
}

nullptr was added in C++ 11 so that the compiler would know the difference between calling these two functions.

void setValue(int value);

void setValue(int* pointer);
aj.toulan
  • 1,341
  • 1
  • 16
  • 25