Thread: Error mesg in program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    Error mesg in program

    Hi

    i am trying to call this search function in the main,

    Code:
    int search(int a[], int v, int l, int r)
    { int i;
    for (i = l; i <= r; i++)
    return i;
    return -1;
    }
    however i get an error for the following:

    if (v == a[i])

    i am not sure why i get that error

    thnx

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Possibly because your counter should go fro 0 to < r. Arrays are indexed from 0.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please post a code sample that acutally contains the line(s) with the problem...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also, try and be more specific than "an error"
    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.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Can you please make sure code is properly indented? It's easier for us to help you.
    Last edited by QuadraticFighte; 10-23-2010 at 02:53 AM. Reason: Oops, I read as 1 instead of ell

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    yes...

    sorry i fogot to specify the error....

    when i run it...theres a segmentation fault.
    however, when i run it using gdb i get the following about that line

    Program received signal SIGSEGV, Segmentation fault.
    0x0804849b in search (a=0xbfcff524, v=2, l=-1076890280, r=134513913)



    thanks

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Did you do what nonoob suggested?
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by papermate View Post
    yes...

    sorry i fogot to specify the error....

    when i run it...theres a segmentation fault.
    however, when i run it using gdb i get the following about that line

    Program received signal SIGSEGV, Segmentation fault.
    0x0804849b in search (a=0xbfcff524, v=2, l=-1076890280, r=134513913)



    thanks
    I will take a wild stab in the dark and say that you have uninitialized values, because l is negative and hugely off-base while r is positive and hugely off-base.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    ok...this is what i have done in the main

    and it still doesnt work, also is my passing on correct

    Code:
    int main( int argc, char *argv[]) {
        int a[]= {1,4,5,8,7};
        int v=1;
        int l, r, i;
    
        printf("Enter an integer:\n");
        scanf("\n%d", &v);
    
        search(a,  v, l,r);
        if(a[i]== v) {
          printf("Found\n");
        } else {
          printf("Not found\n");
        }

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The function search, needs to have it's code, *outside* of main(). The line that begins the function search, should be copied up above main() (not into main(), but above it), as a function prototype.

    All your values for the parameter of search, need to have been assigned a value, before search is called in main().

    You need to re-read your text, clearly. There's an awful lot of basic info that you have not yet learned, and you can't program anything like this, without that basic knowledge.

    More study!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > and it still doesnt work, also is my passing on correct
    And where are l and r initialised (as per QuadraticFighter's previous comment)?

    Simply declaring variables with the same name as the parameters in the function doesn't work.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM