Like Tree1Likes
  • 1 Post By Click_here

Pointer to Array Problem, Urgent Help plz

This is a discussion on Pointer to Array Problem, Urgent Help plz within the C Programming forums, part of the General Programming Boards category; Hi, Im using pointer to display values of an array but its output is not correct example 23424,0, -323423 etc... ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Angry Pointer to Array Problem, Urgent Help plz

    Hi, Im using pointer to display values of an array but its output is not correct example 23424,0, -323423 etc... whats the problem with my code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    clrscr();
    
    int arr[10][10],rows,cols;
    int* ptr;
    
    printf("\nNo. of Rows: ");
    scanf("%d", &rows);
    printf("\nNo. of Cols: ");
    scanf("%d", &cols);
    
    
    //storing values in array
    for(int i=0; i<rows; i++)
        for(int j=0; j<cols; j++)
           {
        printf("\nEnter element Row %d Column %d: ", i,j);
        scanf("%d", arr[i][j]);
           }
    //displaying values using pointers
    for(i=0; i<rows; i++)
       for(j=0; j<cols; j++)
       {
        ptr=&arr[i][j];
         printf("\n%d", *ptr);
    
       }
    
    getch();
    
    }
    Last edited by shansajid; 02-12-2013 at 10:04 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,869
    You fail to use a pointer in scanf, then the pointer for printf was quite unnecessary (especially since you dereference it in the end).
    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

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    Code:
    scanf("%d", &arr[i][j]);
    shansajid likes this.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    laserlight -> I didn't even type anything and you beat me!!!
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    yes, it was problem of scanf. thanks ...!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with pointer in array
    By hugoguan in forum C Programming
    Replies: 7
    Last Post: 11-30-2010, 02:13 AM
  2. Pointer array problem
    By ernielam1024 in forum C Programming
    Replies: 10
    Last Post: 06-17-2006, 10:26 AM
  3. Pointer&Array problem
    By LloydUzari in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2004, 03:18 PM
  4. problem with pointer/array
    By razza in forum C Programming
    Replies: 9
    Last Post: 05-22-2002, 01:20 PM
  5. Replies: 1
    Last Post: 09-30-2001, 08:45 AM

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