Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with array of pointers

Status
Not open for further replies.

Lekar

Programmer
Sep 7, 2001
85
0
0
CR
I have a problem with the third element from notas:
Code:
#include <iostream.h>
#include <conio.h>

struct nota_musical
{
  char *nota;
};


int main()
{
  clrscr();
  // Array of pointers
  nota_musical *notas[7];

  char *notmusic[7] = {&quot;do&quot;, &quot;re&quot;, &quot;mi&quot;, &quot;fa&quot;, &quot;so&quot;, &quot;la&quot;, &quot;si&quot;};

  for (int i = 0; i < 7; i++)
       notas[i] -> nota = notmusic[i];
  for (int j = 0; j < 7; j++)
   cout << &quot;notas[&quot; << j << &quot;] -> nota &quot;  << notas[j] -> 
            nota << endl;
  getche();
  return 0;
}
When I run this code this appears :
Code:
notas[0] -> nota do
notas[1] -> nota re
notas[2] -> nota
notas[3] -> nota fa
notas[4] -> nota so
notas[5] -> nota la
notas[6] -> nota si
I can't find why the notas[2] element couldn't be assigned to the corresponding note (it says that it has a null value), and when I press a key to exit the program, it displays:
Code:
 &quot; Abnormal program termination
   Null pointer assignment &quot;
Where's the problem?
(I don't know if it this can be helpful but I'm using Borland Turbo C++ 3.0)

Thank you very much.
 
I think the reason that this doesn't work is because the line:

nota_musical *notas[7];

only creates an array of pointers to structure type nota_musical. The actual structures don't exist anywhere in memory so when you try to assign to them with,

for (int i = 0; i < 7; i++)
notas -> nota = notmusic;

there's no structure to actually assign to.

The only thing I don't understand how your program worked long enough to generate any output at all.

paulf
 
I thinkthat when you remove the * in
nota_musical *notas[7];
you should be fine...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top