Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello guys,
I create a program in c and I want to use dynamic pointers. The problem is the following....

I have a txt file that contains only two lines of numbers. In the first line there is only one number that says how big must be the array and how many numbers should hold.
In the second line there are as many numbers as the first line say.
-----------------
C++
Eg. file.txt 
9 
982 82 89418 929 98 419925 98 41280 9841 

-------------
As you see the first line says that we want to store 9 numbers and in the second line we have the 9 numbers we should store.

The problem is that the number 9 can be from 1 to 250.000 which I can't create a code to store so many numbers.

How can I store until 250.000 numbers in an dynamic array of pointers. Of course when I'll store the numbers i need a different address for each number so in the next part of my code i could call any number i want. How am I suppose to do that?

I read the following courses, but I couldn't understand how am I suppose to do it.
http://www.programiz.com/c-programming/c-dynamic-memory-allocation[^]
Posted
Comments
Sergey Alexandrovich Kryukov 7-Nov-14 9:53am    
The manual you referenced looks clear enough; there are many others. What is your problem, exactly?
What have you tried so far?
—SA
[no name] 7-Nov-14 10:00am    
before i'll post the code...first i have question. Do I need to put the numbers in one column and the variable part will be the rows?
Sergey Alexandrovich Kryukov 7-Nov-14 10:32am    
What do you mean by "column"? I see no columns. As I understand, first number of first line specifies the number of numbers in second one. Very good. Read/parse this number, take sizeof your numeric type, multiply them. It gives you the number of bytes to allocate. Then allocate the memory for your array.
—SA
[no name] 7-Nov-14 10:02am    
well i tried to to make one column and the variable part to be the rows. So i'll have 1D table. The problem I have is eplained very clearly. I will post my code anyway
[no name] 7-Nov-14 10:08am    
If I post my code i need to explaine all of that cause it's a very large program. So i tried to specify my problem.

1 solution


  1. Read the first line and convert it to an integer.
  2. Use malloc to create a pointer to an array of integers. The array length is set from the number first read.
  3. Read the remaining fields from the text file one line at a time.
  4. Use strtok to split each line into items.
  5. Convert each item to an integer and store in the next free element of the array.
  6. Repeat until all items have been processed.

The documentation for all functions (malloc, strtok etc.) can be found at http://msdn.microsoft.com/en-us/library/634ca0c2.aspx[^].
 
Share this answer
 
Comments
[no name] 7-Nov-14 10:27am    
ok but I have only two lines. The second line just hold the numbers. Did you mean that? or you mean that I have many lines of numbers? -so I would need many columns-
Richard MacCutchan 7-Nov-14 10:34am    
It does not matter whether the numbers are all in one line or many lines, you still only need a single one dimensional array to hold them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900