Thread: help in array

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    35

    help in array

    hi frnds i have a prob in a question actually in this question iwant to pass part of an array to a function but i cant understand how can i pass my atempt is


    Code:
    #include<stdio.h>
    #include<conio.h>
    void part(int(*f)(int a[5]));
    main()
    {
    clrscr();
    int i;
    int a[5]={1,2,3,4,5};
    {
    for(i=0;i<5;i++)
    printf("%d",a[i]);
    }
    void part(int(*f)(int a[i]));
    getch();
    }
    void part(int(*f)(int a[5]))
    {
    int(*a[])()={
    	     for(i=2;i<=3;a++)
    	     printf("%d",a[i]);
    	     }

    tell me how can i solve my problem

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int a[5]={1,2,3,4,5};    // this is your array
    
    void part( int a[5] );   // this is the prototype
    
    part( a );  // this is a call to the function - note, no [ ]
    
    void part( int a[5] ) {  // this is the definition
      int i;
      for(i=0;i<5;i++)
        printf("%d",a[i]);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    35
    hey i know but i have to pass just a part of array not the whole array

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You should indent correctly.
    Code:
    #include<stdio.h>
    Ewwww.
    Code:
    #include <cstdio>
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent correctly.
    To be fair, this is a matter of style, and except from the change from <stdio.h> to <cstdio>, it does not matter at all.

    Consider a sample program probably written by Bjarne Stroustrup: How do I write this very simple program?

    hey i know but i have to pass just a part of array not the whole array
    Pass two pointers: one points to the start of the range, the other to the end (or one past the end, to follow a general idiom of the C++ standard library). Alternatively, you can pass the array, and two indices: one to the start of the range, and the other to the end (you get the idea).
    Last edited by laserlight; 01-16-2007 at 06:50 AM.
    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

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    In Bjarne's case, there is actual indentation. Here is an ugly mess. I'll highlight in red everything that is wrong:
    Code:
    #include<stdio.h>     //See my post. Also, should be <iostream>.
    #include<conio.h>     //Nonstandard, consider replacing.
    void part(int(*f)(int a[5]));     //See Salem's post.
    
    main()     //SHould be int main().
    {
         clrscr();
         int i;
         int a[5]={1,2,3,4,5};
         {     //Move one line down.
         for(i=0;i<5;i++)
              printf("%d",a[i]);     //Why do this in C++? As I said, use <iostream>
         }
         void part(int(*f)(int a[i]));     //Again
         getch();
    }
    
    void part(int(*f)(int a[5]))
    {
         int(*a[])()=     //No clue what you're trying to do, so I'll guess.
         {            
              for(i=2;i<=3;a++)
    	          printf("%d",a[i]);
    	     }     //Again, see Salem's post.
    }     //You need this closing brace.
           //For all of this, you need indentation, which I've added.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > hey i know but i have to pass just a part of array not the whole array
    Well
    part( a );

    Is the same as
    part( &a[0] );

    There is no such thing as part of an array, just a pointer to some element of the array, usually the first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    35
    hey no one of u can understand wat em i talking bout actually look actually array is e.g
    Code:
    a{i]={0,1,2,3,4,5,6,7,8,9}
    now i have to right a program in which i have to pass for example just
    Code:
    a[i]={3,4,5}
    not the whole elements of actually array

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hey no one of u can understand wat em i talking bout
    I have the general idea, but passing a "sub-array", if you will, is not quite right. Rather, you should pass pointers to a range in the array.

    Consider the approach demonstrated in this program:
    Code:
    #include <iostream>
    
    void print(int* begin, int* end);
    
    int main()
    {
        int[] numbers = {0,1,2,3,4,5,6,7,8,9};
        // Pass pointers to the array to denote a range.
        // (numbers + 3) is the start of the range {3,4,5}
        // (numbers + 6) is one past the end of the range
        print(numbers + 3, numbers + 6);
    }
    
    // Prints range denoted by [begin, end).
    void print(int* begin, int* end)
    {
        for (int* i = begin; i != end; ++i)
        {
            std::cout << *i << " ";
        }
        std::cout << std::endl;
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM