-4

I have a header file abc.h

//abc.h
unsigned int *get(void)
{
   static unsigned int input[] =
   {
      1,
      2, 2,
      4, 5,
      6, 7
   };
return input;
}

Now, i want to read this input(from header file,not from some text file) into my main cpp file say xyz.cpp

I am thinking of using an array to access these elements,but i don't think it will work.

int arr[6];
arr=get();

The first element is number of test cases n,the second and third element are dimensions of 2-D array and the rest of the elements are the values of 2-D array.So i need to input value of n,rows,columns and values for 2D array arr[rows][columns]

Any ideas on how can i achieve this?

EDIT: I seriously can't figure out why this question is getting downvoted. I agree this is not a good implementation,but I have been given an input header file and i can read data only through this header file!!

techriften
  • 421
  • 2
  • 16
  • 3
    Don't put function implementations in header files. – Jabberwocky Oct 21 '14 at 09:34
  • I agree,but i have been given this external input file and the only way to read input is through this file – techriften Oct 21 '14 at 09:37
  • Why use an array at all? If those integers have any meaning you should consider writing a class to hold them. – user657267 Oct 21 '14 at 09:37
  • @user657267 The point is, I have been given this situation.I have to read input from this header file,do some calculations in main and print answers.That's it. – techriften Oct 21 '14 at 09:40
  • 2
    The idea of putting functions in header files, the idea of returning a pointer to private data, all suggests that the program design is fundamentally flawed. You need to point out that whoever gave wrote this code is incompetent and that their program needs to be fixed. Don't try to solve this by wrapping this bad code up in even more bad code. – Lundin Oct 21 '14 at 09:42
  • 4
    @techriften - Looks like there's a typo, the `get()` function returns `data` but has a static array called `input`: should the last line be `return input;`? Or have you edited something out? – AAT Oct 21 '14 at 09:42
  • @AAT Yes, that was a typo. Corrected now – techriften Oct 21 '14 at 09:46
  • 2
    As for the down votes, they likely come from you dumping some code on the site which is unformatted, unindented, doesn't make sense and won't compile. – Lundin Oct 21 '14 at 09:47

1 Answers1

3

If you able to compile your programm with this file, you does not need to read anything. This array and it values will be compiled into your programm and you can access them right in place

Your xyz.cpp file should look like:

#include "abc.h" // given abc file located in the same directory

int main(){
   unsigned int * myArrayPtr = get();
   // here comes some processing and, if you want, reading values from this array;
   unsigned int numberOfCases = myArrayPtr[0];

   unsigned int * dimensionsArrayPtr = myArrayPtr + 1;
   unsigned int xArraySize = dimensionsArrayPtr[0];
   unsigned int yArraySize = dimensionsArrayPtr[1];

   // and etc.
   // Most interesting part to represent those values as two dimensional array
   // I left to you :)       
   return 0;
}

Also, you should remeber that this trick could work only because array in header file declared as static. Otherways your programm would have undefined behavior.

One more also. If your function body defined in header file, you should declare it inline. As long as this header included only on one cpp file - its all right. But when it will included to more the one code file, you will get already defined linker error.

I recommend you to learn more about pointers in cpp. This article is fine enough http://www.cplusplus.com/doc/tutorial/pointers/

About static keyword - to fully understand this example - there is fine answer on SO itself The static keyword and its various uses in C++

Community
  • 1
  • 1
Vasilly.Prokopyev
  • 856
  • 1
  • 10
  • 24
  • Thanks!!! This is what i was looking. Instead of downvoting,people could have actaully answeed this question. Anyways, thanks :) – techriften Oct 21 '14 at 09:53
  • This question shows, that you actually didnt tried to find answer by yourself. Those links I provided, are almost first results in google by queries "c++ pointers", "c++ static". That is what made your question downvoted. Anyway, I hope my answer is usefull :) – Vasilly.Prokopyev Oct 21 '14 at 09:56