Thread: pointers to an array of type def struct not allowing writing to array, I think

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    27

    pointers to an array of type def struct not allowing writing to array, I think

    Dear All
    It is the last part of this code that is not working. The function removNb(Length, inner); makes many returns to a structure variable called "pair" The first variable in "pair" is tested to see if its value is above zero. If it is the variable "pair" is written to an array of pair (the data type is called Pair that has 2 variables both of which are long long), via a pointer.

    I know that only 2 returns qualify to be written to the array. Before being written to the array they are printed out (they are correct), then they are written to the array and a count of the array element position is incremented.

    To confirm this I then printed out the content of the array of pair via 2 print statements. The expected output is:
    15 = 21
    21 = 15
    But the actual output is:
    15 = 21
    21 = 734423328283964721

    So I am stuck again. Any help appreciated

    Code:
    /*IsMyFriendCheatingV5.C */
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct
    {
        long long first;
        long long snd;
    } Pair;
    //
    Pair removNb(long long length, int n) //
    {   //printf("%d ", n );
        //printf("%lld ", length ); //650
        Pair fpair; fpair.first = 0; fpair.snd = 0;
        long long value =  ( length * (length+1) ) / 2 ;
        //printf("%lld ", value );// 351
        static int MultiplyBy = 2;
        //printf("%d - %d\n",MultiplyBy, n); // 2-26 , 1-26
        if(MultiplyBy*n == value-(MultiplyBy+n) && length == n )
        {
            fpair.first = MultiplyBy; fpair.snd = n; //printf("(%lld-%lld) ", fpair ); // nothing
            MultiplyBy++;
            return fpair;
        }
        else if( MultiplyBy*n == value-(MultiplyBy+n) && length != n )
        {   //printf("%d-%d", MultiplyBy, n );
            //printf("%ld ", n );
            fpair.first = MultiplyBy; fpair.snd = n; //printf("%lld-%lld ", fpair ); // 15-21 21-15
            return fpair;
        }
        else if( MultiplyBy*n != value-(MultiplyBy+n) && length == n )
        {
            //fpair.first = 0; fpair.snd = 0; //printf("(%lld-%lld) ", fpair ); // nothing
            MultiplyBy++;
            return fpair;
        }
    }
    //
    int main()
    {
        Pair ArrayOfpair[] = {}; Pair pair;
        long long Length = 26; int position = 0;
        Pair *PtrOfPair  = (Pair *)malloc(sizeof(Pair));
    //
        for(int outer = 2 ; outer < Length+1 ; outer++)// 2-26, 25 iterations
        {   //printf("%d \n",outer); // 2-26
            for(int inner = 1 ; inner < Length+1 ; inner++)// 1-26, 25 iterations
            {
                //printf("[%d ] ",inner);
                //pair.first = 0; pair.snd = 0;
                pair = removNb(Length, inner);
                //printf("pair: [%lld - %lld]\n", pair);
                if(pair.first > 0)
                {
                    printf("[%lld - %lld]\n", pair);
                    PtrOfPair[position] = pair;  printf("%d\n",position);
                    //printf("%lld=%lld", ArrayOfpair[position]);
                    position++;
                }
            }
        }
        printf("%lld = %lld\n", PtrOfPair[0]);
        printf("%lld = %lld\n", PtrOfPair[1]);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    That code doesn't compile on my machine. Here is what my compiler says:

    Code:
    ||=== Build: Debug in chomework (compiler: gcc9-2) ===|
    //main.c|10|warning: no previous declaration for ‘removNb’ [-Wmissing-declarations]|
    //main.c||In function ‘main’:|
    //main.c|40|error: ISO C forbids empty initializer braces [-Wpedantic]|
    //main.c|40|error: zero or negative size array ‘ArrayOfpair’|
    //main.c|54|warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘Pair’ {aka ‘struct <anonymous>’} [-Wformat=]|
    //main.c|54|warning: format ‘%lld’ expects a matching ‘long long int’ argument [-Wformat=]|
    //main.c|61|warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘Pair’ {aka ‘struct <anonymous>’} [-Wformat=]|
    //main.c|61|warning: format ‘%lld’ expects a matching ‘long long int’ argument [-Wformat=]|
    //main.c|62|warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘Pair’ {aka ‘struct <anonymous>’} [-Wformat=]|
    //main.c|62|warning: format ‘%lld’ expects a matching ‘long long int’ argument [-Wformat=]|
    //main.c|40|warning: unused variable ‘ArrayOfpair’ [-Wunused-variable]|
    //main.c||In function ‘removNb’:|
    //main.c|36|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build failed: 2 error(s), 9 warning(s) (0 minute(s), 0 second(s)) ===|

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,558
    By the way, did you really name the source file "IsMyFriendCheatingV5.C", as in with a ".C" (uppercase) rather than ".c" (lowercase) name suffix/extension? If so, you should be aware that some compilers, notably gcc, will compile your source file as C++ instead of C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    Thank you both

    What was
    for(int outer = 2 ; outer < Length+1 ; outer++)
    {
    became an error after I changed the .C to .c. It was resolved by coding:
    int outer;
    for(outer = 2 ; outer < Length+1 ; outer++)
    {
    After making changes to 2 for loops as above, the programme compiled and Ran and produced exactly the same error.
    The IDE is Code::Blocks v13:12, with GNU GCC Compiler selected from a very long list of compliers.
    I am at a loss to know what to do about the fact that it does not compile on jimblumberg’s machine.

    Any further help even more gratefully appreciated

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    became an error after I changed the .C to .c. It was resolved by coding:
    Then perhaps you need to set the compiler to use the c11 standard, for a start.

    After making changes to 2 for loops as above, the programme compiled and Ran and produced exactly the same error.
    Also what about all those other problems I pointed out?

    Those printf() format issues need to be treated like errors and fixed. By the way how is printf() supposed to know how to print a pair?

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    Dear jimblumberg,

    Thank you. I went to the application and clicked Settings > Compiler > then the drop down list of what I think are compilers. But no C11 option to select. After Laserlight’s suggestion I went to the drop down list of what I think are Compiler, and selected “Inter C/C++ Compiler” and ran the amended code again. There was no difference. I went back to the compiler list and it had reverted to GNU GCC Compiler. I mention this as simply selecting from the list does not work. It is more complicated than I know at the moment.

    Regarding: “Also what about all those other problems I pointed out?” Well, I am out of my depth and knowledge, sorry.

    Regarding: “Those printf() format issues need to be treated like errors and fixed”. Yes I am sure you are right but at the moment I could not validate a change as I have not changed the complier and do not know how to. I was hoping that changing from C to c was going to solve many issues.

    Regarding: “By the way how is printf() supposed to know how to print a pair?” I think the answer is:
    printf("%lld = %lld\n", PtrOfPair[0]);
    element 0 is a structure containing 2 fields and each is formatted with a %lld. So you see 2 occurrences of %lld in the printf.

    Any further help even more gratefully appreciated

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    27
    I am reposting the above response so as to make it more readable

    Dear jimblumberg,

    Thank you. I went to the application and clicked Settings > Compiler > then the drop down list of what I think are compilers. But no C11 option to select. After Laserlight’s suggestion I went to the drop down list of what I think are Compiler, and selected “Inter C/C++ Compiler” and ran the amended code again. There was no difference. I went back to the compiler list and it had reverted to GNU GCC Compiler. I mention this as simply selecting from the list does not work. It is more complicated than I know at the moment.

    Regarding:
    Also what about all those other problems I pointed out?
    Well, I am out of my depth and knowledge, sorry.

    Regarding:
    Those printf() format issues need to be treated like errors and fixed
    . Yes I am sure you are right but at the moment I could not validate a change as I have not changed the complier and do not know how to. I was hoping that changing from C to c was going to solve many issues.

    Regarding:
    By the way how is printf() supposed to know how to print a pair?
    I think the answer is:
    printf("%lld = %lld\n", PtrOfPair[0]);
    element 0 is a structure containing 2 fields and each is formatted with a %lld. So you see 2 occurrences of %lld in the printf.

    Any further help even more gratefully appreciated

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    Thank you. I went to the application and clicked Settings > Compiler > then the drop down list of what I think are compilers. But no C11 option to select.
    What compiler/IDE are you using?

    If you are using some version of CodeBlocks then you need to right click on the project in the Project Management folder, then select Build Options.

    I suggest you "select" most of the check boxes in the Warnings section and also the "Produce debugging information" check box. As for setting the standard you need to select the "Other Compiler Options" tab and type in "-std=c11", I'd also add another item here as well "-Wvla"

    Here is what I use for compiling most C programs from forums:

    -Wall -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wfloat-equal -Winline -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Wmain -pedantic-errors -pedantic -Wextra -Wall -g -Wvla -std=c11
    I think the answer is:
    Nope, you need to print the individual values, there is no format specifier to handle a user defined type like pair.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing an array of struct to a binary file
    By alien1 in forum C Programming
    Replies: 6
    Last Post: 03-19-2015, 02:33 PM
  2. Accessing an Array of Struct type in Function
    By Euph11 in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2013, 04:52 PM
  3. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM
  4. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM
  5. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM

Tags for this Thread