Double Pointer Trouble

This is a discussion on Double Pointer Trouble within the C Programming forums, part of the General Programming Boards category; The following programme is supposed to use pointers totype double variables to accept 10 numbers, sort them, print them. Code: ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Double Pointer Trouble

    The following programme is supposed to use pointers totype double variables to accept 10 numbers, sort them, print them.

    Code:
    /* D14Ex7.c - programme to use pointers to type double to accept 10 numbers *
     * from the user, sort them and print them to the screen. */
    
    #include <stdio.h>
    
    double *ptr[10];
    
    int main ( void )
    {
            int cnt;
            printf("\t\t\t====You must enter 10 numbers====\n");
            for (cnt = 0; cnt < 10; cnt++)
            {
                    printf("\nEnter number %d: ", cnt);
                    scanf("%lf", ptr[cnt]);
            }
    
            for (cnt = 0; cnt < 10; cnt++)
            {
                    printf("\n%f", *ptr[cnt]);
            }
            return 0;
    }
    Sorry if it's not formatted well, I'm using a text-based brwser
    I'll check later to see if I need to repost the code. The poblem is that after entering a first number, the programme generates a `segmentation fault' error and goes to the shell.
    Any suggestions?
    Last edited by Rhodium; 05-19-2003 at 06:04 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You have to allocate memory for each of the 10 slots in the array. Look up malloc( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorry if it's not formatted well, I'm using a text-based brwser
    That's no excuse. Use code tags. (IE: Read that nice little thread that tells you how to do them.)

    [code]
    ...your code here...
    [/code]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    OK. There's the CODE tag.

    Anyhow, Thanx XSquare, That problem is solved now.

    However, another one's come up:
    Could someone tell me what's wrong with this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void sort(double *ptr[]);
    
    int main ( void )
    {
            double *ptr[10];
            int cnt;
            for (cnt = 0; cnt < 25; cnt++)
                    printf("\n");
    
            printf("\t\t\t\t=========You must enter 10 numbers=========\n");
            for (cnt = 0; cnt < 10; cnt++)
            {
                    ptr[cnt] = malloc(10*(sizeof(double)));
            }
    
            for (cnt = 0; cnt < 15; cnt++)
                    printf("\n");
    
            for (cnt = 0; cnt < 10; cnt++)
            {
                    printf("\nEnter number %d: ", cnt+1);
                    scanf("%lf", ptr[cnt]);
            }
    
            sort(ptr);
            printf("\n");
            return 0;
    }
    
    void sort(double *ptr[10])
    {
            int a;
            double x;
    
            printf("<<SORTING>>");
            for(a = 0; a < 10; a++)
            {
                    if(*ptr[a] < *ptr[a+1])
                    {
                            x = *ptr[a+1];
                            *ptr[a+1] = *ptr[a];
                            *ptr[a] = x;
                    }
            }
    
            printf("\n");
            for(a = 0; a < 10; a++)
            {
                    printf("\t%f\n", *ptr[a]);
            }
            printf("\n");
    
    }
    Any help?
    Thanx
    Thanx
    Rhodium

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>ptr[cnt] = malloc(10*(sizeof(double)));
    Should be just sizeof( double ). You only want 1 double per slot, not 10.

    Since you're using bubble sort, you should also have 2 for loops in your sort function. Otherwise it won't get sorted.

    Pseudocode:
    Code:
    for each pass from 1 to n
        for each element from 1 to n - 1
            if the current element is less than the number after it
                swap them
            end if
        next element
    next pass
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Also, you aren't free( )ing your memory that you malloc( )ed.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) In your loop, you realize that you are allocating 10 doubles for each pass through the loop, right? I hope that was intended.

    2) Your next loop only enters ten numbers. One into each element of the loop. You have 90 elements unused.

    Fix the above first.

    Additionally, don't just say "What's wrong with my code." No one here wants to read hundreds of lines of code looking for some obscure problem because you haven't taken the time to simply provide:

    "My program does[n't do] this."
    "My program [should|does] do this."
    "I am getting [this error|no errors]."

    If you want help, you need to provide as much information as possible.

    [edit]Beat me to it.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Lightbulb Thanx

    Originally posted by Salem
    You've got one too many levels of indirection
    The purpose of the programme was practicing and experimenting with pointers!!!

    Originally posted by quzah
    ... provide:

    "My program does[n't do] this."
    "My program [should|does] do this."
    "I am getting [this error|no errors]."

    If you want help, you need to provide as much information as possible.
    Originally posted by Rhodium
    The poblem is that after entering a first number, the programme generates a `segmentation fault' error and goes to the shell.
    Thanx for all the helps...appreciated.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You quote your original problem, and yet you post two seperate code instances. In the first example, you didn't allocate any memory. That's your first problem.

    In your second example, you didn't allocate memory correctly. You still haven't fixed that problem. There's nothing more to help, short of doing all your work for you. There are easily enough code examples and comments for you to resolve the problem.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer with double asterisk
    By skringla in forum C Programming
    Replies: 10
    Last Post: 11-27-2008, 06:33 AM
  2. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 03:32 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21