Thread: pointers and referencing

  1. #1
    Registered User
    Join Date
    Oct 2002

    Unhappy array of pointers wont compile fatal error

    The question on my problem set asks to compute the dot product with one of the arguments being a pointer to a pointer
    the two vectors are stored as rows of an array 2xn
    My code looks like this but its giving me fatal errors:

    #include <stdio.h>
    #include <stdlib.h>
    double dotprod1(int n, double **a) /*n is the vector length*/
        int i, j;
        double dtprd1;
     a = calloc(n, sizeof(double *));
     printf("Please enter vectors:\n");
     for (i=0; i<2; ++i)
        for (j=0; j<n; ++j)
           scanf("%lf", a[i][j]);
    for (j=0; j<n; ++j)
      dtprd1 += a[1][j] * a[2][j];
    return dtprd1;
    any clues where i went wrong i must admit i dont understand pointers and arrays well

    Code Tags added by kermi3
    Last edited by nata; 10-21-2002 at 09:09 PM.

  2. #2
    for starters u dont have a main function

  3. #3
    Registered User
    Join Date
    Oct 2002
    this is true! (i admit i dont know the basics yet) but the function that is there has something wrong with its referencing

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    cout << "No code tags are bad";

    With Code Tags:
    for(int i=0;i<5;i++)
         cout << "This code is easy to read";
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    Well the main() thing is really basic. But the reason you are having problems is because of this:

    int **x = (int **)malloc(2 * sizeof(int *));
    /*this allocates an array of pointers (2 pointers to be exact*/
    x[0] = (int *)malloc(sizeof(int));
    x[1] = (int *)malloc(sizeof(int));
    /*this allocates the actual ints that you are going to work with*/
    The point is that you are not allocating the memory required to an operation like
    x[0][1] = 65;
    You are must do the second malloc() or calloc().

    int **x = (int **)calloc(2, sizeof(int *));
    x[0] = (int *)calloc(1, sizeof(int));
    x[1] = (int *)calloc(1, sizeof(int));

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  2. Replies: 18
    Last Post: 06-14-2003, 10:40 AM
  3. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM
  4. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM