Dissappearing data in array...

Posted on 2001-06-20
Last Modified: 2010-04-02
I am using MS Dev C++ 6.0 under Win2k.  The program is a simple console program to split up a data set into 12 frames and will rearrange the data.  The problem is as follows.  Where I know stuff is ok I have summarized.  The problem is that depending where I do the cout statement the data I dumped into the array is printed.  The values are read from a file where some characters have to be skipped.  That is fine.  The output should be values like 1.0013 and so on.

main() ...
char *arrIJK[40][54][54];
open file...
for loops for k < 40 and j < 54 with k++ and j++
  for(int i=0; i < 54; i++)
    fgets(tmp_buffer, 3, r_file);
    fgets(tmp_buffer, 7, r_file);
    arrIJK[k][j][i] = tmp_buffer;
    cout << arrIJK[k][j][i];      // prints ok
    fgets(tmp_buffer, 3, r_file);
    cout << arrIJK[k][j][i];      // prints blank
Question by:ancient1

Accepted Solution

MDarling earned 300 total points
ID: 6209734
Are you using the tmp_bufrer over and over again and expecting the data to persist?

You have a 3 dimensional array of char pointers.

All you seem to be doing is setting each element in turn to point to the same buffer.

Is this what you want?

I'm guessing it's printing blank because it's read blank.

Try this...

cout << "tmp_buffer is " << tmp_buffer << endl
     << "arrIJK[k][j][i] is " << arrIJK[k][j][i] << endl;

and see if you don't get blanks for both.

Fundamentally, though, your use of the pointer array looks flawed.  You need to dynamically allocate a tmp_buffer for each element.

LVL 30

Expert Comment

ID: 6210055
Please post the comple code for main(), even if you think it's unrelated to the problem.
LVL 14

Expert Comment

ID: 6210226
>>arrIJK[k][j][i] = tmp_buffer;
error, becouse you change tmp_buffer all time (and
change arrIJK[k][j][i] in parallel.
Right code is:
arrIJK[k][j][i] = strdup(tmp_buffer);
and after end of function you must free data:

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 14

Expert Comment

ID: 6240613
ancient1, I hope , you use my way to store data.
LVL 11

Expert Comment

ID: 7014548
Dear ancient1

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept


comment(s) as an answer and you never provided any feedback.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is:


Expert Comment

ID: 7030983
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question