Thread: what is wrong here???

  1. #1
    Unregistered
    Guest

    Post what is wrong here???

    in my assigment i have to use pointers but it doesnt work

    #include <stdio.h>
    #include <conio.h>

    int a[5][5],*p;

    void init(int *p) { //error
    int i,j;
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    *(p+(i*5+j))=j;
    }
    void load(int *p) { // error
    int i,j;
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    *(p+(i*5+j))+=i+1;
    }

    void print(int *p) { // error
    int i,j;
    for(i=0;i<5;i++) {
    for(j=0;j<i;j++)
    printf("%d",*(p+(i*5+j)));
    printf("\n");
    }
    }
    void main() {
    int i,j;
    clrscr();
    p=&a[0][0];
    init(&p); //error
    load(&p); //error
    print(&p); //error
    getch();
    }

    how can i fix this??

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: what is wrong here???

    Originally posted by Unregistered
    in my assigment i have to use pointers but it doesnt work

    #include <stdio.h>
    #include <conio.h>

    int a[5][5],*p;

    void init(int *p) { //error
    int i,j;
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    *(p+(i*5+j))=j;
    }
    void load(int *p) { // error
    int i,j;
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    *(p+(i*5+j))+=i+1;
    }

    void print(int *p) { // error
    int i,j;
    for(i=0;i<5;i++) {
    for(j=0;j<i;j++)
    printf("%d",*(p+(i*5+j)));
    printf("\n");
    }
    }
    void main() {
    int i,j;
    clrscr();
    p=&a[0][0];
    init(&p); //error
    load(&p); //error
    print(&p); //error
    getch();
    }

    how can i fix this??
    1) Can you please tell us what errors you get?
    2) void main() is bad style... use int main()
    3) Why do you have i and j in the main function?
    4) p is a pointer, so when you use &p as a parameter you send the adress of the pointer itself, not what it's pointing at.
    5) Please use code tags next time
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    int main() { 
     int i,j; 
     clrscr(); 
     p=&a[0][0]; 
     init(p);
     load(p);
     print(p);
     getch(); 
    }
    or...
    Code:
    int main() { 
     int i,j; 
     clrscr(); 
     init(&a[0][0]);
     load(&a[0][0]);
     print(&a[0][0]);
     getch(); 
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    p = a[0];
    init(p);	
    load(p);	
    print(p);
    It compiles and runs if you do this, but without looking deeper into what your functions are doing (no time at present), I'm not sure if it's safe or not.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    p = a[0]; <--> p = &a[0][0];

    Since arrays are basically a pointer.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You'd better read this
    http://www.cprogramming.com/cboard/s...threadid=17742
    To see why int **a is not the same as int a[10][10];

    Is this a deliberate exercise in squashing 2D arrays into hand-subscripted 1D arrays

    Or because you haven't figured out the syntax for higher dimensions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM