Thread: Array of pointers problem

  1. #1
    Registered User
    Join Date
    Jun 2009

    Array of pointers problem

    I have a prob :
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <time.h>
    using namespace std;
    int AutoFillofArrays(int masyvas[11], time_t sekundes, int i);
    int FillingArraysFromFile(FILE *DuomenuFailas, int *ap[][10], int i, int j);
    int main()
    int a1[11], a2[11], a3[11], a4[11], a5[11], a6[11], a7[11], a8[11], a9[11], a10[11];
    int *ap[][10] = {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10};
    FILE *DuomenuFailas;
    time_t seconds;
    int i, switchmsg, j;
    cout<<"1.Fill array automaticaly\n";
    cout<<"2.Fill array from file\n";
    switch (switchmsg) {
           case 1:
                   AutoFillofArrays(a1, seconds, i);
                   AutoFillofArrays(a2, seconds, i);
                   AutoFillofArrays(a3, seconds, i);
                   AutoFillofArrays(a4, seconds, i);
                   AutoFillofArrays(a5, seconds, i);
                   AutoFillofArrays(a6, seconds, i);
                   AutoFillofArrays(a7, seconds, i);
                   AutoFillofArrays(a8, seconds, i);
                   AutoFillofArrays(a9, seconds, i);
                   AutoFillofArrays(a10, seconds, i);
                   cout<<"Filled automaticaly\n";
           case 2:
                   FillingArraysFromFile(DuomenuFailas, a2, i);
                   FillingArraysFromFile(DuomenuFailas, a3, i);
                   FillingArraysFromFile(DuomenuFailas, a4, i);
                   FillingArraysFromFile(DuomenuFailas, a5, i);
                   FillingArraysFromFile(DuomenuFailas, a6, i);
                   FillingArraysFromFile(DuomenuFailas, a7, i);
                   FillingArraysFromFile(DuomenuFailas, a8, i);
                   FillingArraysFromFile(DuomenuFailas, a9, i);
                   FillingArraysFromFile(DuomenuFailas, a10, i);
                   cout<<"Filled from file \n";
    printf ("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d",(*ap)[1][1], (*ap)[1][2], (*ap)[1][3], (*ap)[1][4], (*ap)[1][5], (*ap)[1][6], (*ap)[1][7], (*ap)[1][8], (*ap)[1][9], (*ap)[1][10]);
    int AutoFillofArrays(int masyvas[11], time_t seconds, int i)
    srand((unsigned int) seconds);
    int FillingArraysFromFile(FILE *DuomenuFailas, int *ap[][10], int i, int j)
    if ((DuomenuFailas=fopen("DuomenuFailas.txt","r"))==NULL)
       {printf("Error opening file \n");
    for (i=0;i!=11;i++)
    fscanf(DuomenuFailas, "%d", ap[1][i]);
    Now, i have 10 int arrays (a1, a2,....,a10) all of them with 10 elements, i cant find a way to edit the FillingArraysFromFile or AutoFillingArrays functions which i wrote there to make them fill those all ten arrays through a array of pointers which i have too (*ap[][10] - [NumberOfAnArray][Element of the array]), i want to shorten up code that i shoudnt need to use those functions on each int array individualy, i mean making a "for" cycle or something like that to fill those 10 arrays in one function at one time though that array of pointers pointing to those 10 arrays. ~.~: damn, its hard to explain i tried few ways but i coudnt make it work.. anyone can edit at least of those two functions to make it work the way i said ?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    As a start...

    Are you trying to write a C++ or a C program? You should decide which one you want and then either stick with C++ I/O (cin/cout) or C I/O (printf, fscanf, etc...).

    srand should be seeded only once at the start of your program, not in the AutoFillofArrays function where it is called multiple times in no doubt quick succession. This means that the sekundes argument to the function is not needed. Also, there is no need to pass in the i variable to the AutoFillofArrays function. It should be local.

    Your AutoFillofArrays function should take an int pointer argument for the arrays that you wish to be changed once the function ends. Your declaration of the first argument to that function as an array and not a pointer means that the function only alters the values in the temp copy of the array that is passed in and not the original array which is what you need.

    Your call of FillingArraysFromFile does not match the prototype or function definition. You call the function with 3 arguments but the prototype and definition indicate 4 arguments. There is no need to pass in the i variable to the FillingArraysFromFile function. It should be local.

    Your FillingArraysFromFile function should take an int pointer argument just like the AutoFillofArrays function should instead of the int *ap[][10] argument it currently does. The fscanf call will need to be reworked (simplified) as well to take this into account.

    Your ap variable is declared wrong, it should be just simply:
    int *ap[10] = {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10};
    ...this also means that your printf function at the end of main needs to be reworked (simplified) if you really want to use that ap pointer array to output the values instead of the array directly.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM