question on pointer to new array structure

Hi
I'm attempting to create a pointer to a memory location for a structure. I am able to create such a pointer for an integer, but having problems with a structure.

The integer example is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main () {

  int *tacos = new int[10];

  *tacos = 0;
  *(tacos+3) = 2;

  std::cout << "Value of tacos = " << tacos << std::endl;
  std::cout << "Value of *tacos = " << *tacos << std::endl;
  std::cout << "Value of *(tacos+3) = " << *(tacos+3) << std::endl;

  std::cout << "Value of tacos[0] = " << tacos[0] << std::endl;
  std::cout << "Value of tacos[3] = " << tacos[3] << std::endl;

  delete [] tacos;
  return 0;
}



No issues with the above. All works well. But the following causes a compile error for line 22 & 23.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

int main() {

  struct choc {
    std::string choc_name;
    float weight;
    int calories;
  };

  choc *citems = new choc[10];


  *citems = {"Mocha Munch",0.3,200};
  *(citems+1) = {"Toffee caramel",0.3,300};

  // choc citem {"Mocha Munch",0.2,200};
  // std::cout << "Name : " << citem.choc_name << " weight " << citem.weight << " calories " << citem.calories << std::endl;

  std::cout << "Name : " << citems->choc_name << " weight " << citems->weight << " calories " << citems->calories << std::endl;
  // std::cout << "Name : " << citems[1]->choc_name << " weight " << citems[1]->weight << " calories " << citems[1]->calories << std::endl;
  std::cout << "Name : " << *(citems+1)->choc_name << " weight " << *(citems+1)->weight << " calories " << *(citems+1)->calories << std::endl;

  delete [] citems;
  return 0;
}


The compile error is:
1
2
choc_bar.cpp:23:29: error: no match for β€˜operator*’ (operand type is β€˜std::__cxx11::string {aka std::__cxx11::basic_string<char>}’)
   std::cout << "Name : " << *(citems+1)->choc_name << " weight " << *(citems+1)->weight << " calories " << *(citems+1)->calories << std::endl;

This gives you a string: (citems+1)->weight

This is you attempting to call the dereference operator, *, on a string: *(citems+1)->weight

Try this instead:

std::cout << "Name : " << (citems+1)->choc_name << " weight " << (citems+1)->weight << " calories " << (citems+1)->calories << std::endl;

The -> operator dereferences the pointer (citems+1)


Basically, you're dereferencing the pointer to get the thing it's pointing at, and then you're trying to dereference that as well.
Last edited on

thanks, modified and it worked,

Why is it that for the new pointer to an integer in the first program I listed I have to use the notation *(taccos+3) to get to the value 2, but for the structure, in the second listing, I have to use (citems+1) ?

Output of the first program is:
1
2
3
4
Value of *tacos = 0
Value of *(tacos+3) = 2
Value of tacos[0] = 0
Value of tacos[3] = 2


Topic archived. No new replies allowed.