testing array

This is a discussion on testing array within the C Programming forums, part of the General Programming Boards category; is there any way to test to see if an element of an array exists in c? I am passing ...

  1. #1
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168

    testing array

    is there any way to test to see if an element of an array exists in c?

    I am passing an array into a function in c and i want to make sure that I am writing to an existing element.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no such way - you need to pass the size of the array to the function so your function knows how large the array is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    ooo i can just use
    if (array[i] != NULL)

    EDIT: oh .. nvm

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No.

    (When passed into a function, an array is turned into just a pointer to the first element; if you want to know the size, you must pass that in to the function separately.)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chico1st View Post
    ooo i can just use
    if (array[i] != NULL)

    EDIT: oh .. nvm
    Well, you CAN if you can guarantee that the last element in the array is always NULL and nothing else in the array is. It is perfectly valid to not pass in the array size if you have a "sentinel" or "end-marker" to indicate the end of the array.

    However, you have to be careful that you do not prevent valid values from being used in the array. An array of unsigned integers that could hold any value would not be possible to do this with, since ALL values would potentially be used.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Wanabe Laser Engineer chico1st's Avatar
    Join Date
    Jul 2007
    Posts
    168
    would this work to find the end of an array... assuming there are no '\0' in the array
    Code:
    uInt8 *image = NULL;
    
    
    FunctionICantSeeInsideOf((void *) &image);
    
    for(i=0; i<(10000000); i++){
    		if(image[i] == '\0')
    		{
    			lengtharray = i;
    			goto over;
    		}
    	}
    over:
    return lengtharray

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chico1st View Post
    would this work to find the end of an array... assuming there are no '\0' in the array
    Code:
    uInt8 *image = NULL;
    
    
    FunctionICantSeeInsideOf((void *) &image);
    
    for(i=0; i<(10000000); i++){
    		if(image[i] == '\0')
    		{
    			lengtharray = i;
    			goto over;
    		}
    	}
    over:
    return lengtharray
    Yes, the use of a "marker" to indicate the end is one way to indicate how long a sequence of data is. However, unless "FunctionICantSeeInsideOf" is putting a the correct marker at the end of the array, you can't use this sort of mechanism.

    And your use of "goto" is completely avoidable and "useless", since "break" is shorter to type and more acceptable. Alternatively, just use "image[i]" as part of the end condition of the for-loop, and set lengtharray after the loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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