Thread: End loop when empty/null value found

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    27

    End loop when empty/null value found

    Hello

    I'm looping through hex values

    Code:
    for (x = 0; x <=10; x++) //Ignore the x <=10 value 		{
    			printf("\nThe Value of hex %x is: %i\n",*(hex+x), *(hex+x));
    			printf("\nThe value of X is: %i\n", x);
    			getchar();
    		}
    And I want it to stop when the test data is depleted

    Code:
    *hex = 0x0F; 	*(hex+1) = 0x11;
    	*(hex+2) = 0xFF;
    	*(hex+3) = 0x1B;
    	*(hex+4) = 0x00;
    	*(hex+5) = 0xB6;
    How would I go about that? As 0x00 can actually be a value, I can't stop looping once 0x00 condition is met

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When will the test data be "depleted"?
    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
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    For certain your question doesn't make much of sense thus far. Indeed you may be well assign hex value as you've shown above.

    Are you asking that loop has to terminate when i == 0x00? If it is, then it not much of an expensive task to accomplish.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Test data can be as big or as small.

    I suppose my question would be, what value would I be looking to detect as a end loop requirement when I have not actually allocated a value to *(hex+x)

    For example, I'd want the below to end at the equivalent of *(hex+6)

    Code:
    *(hex+2) = 0xFF; *(hex+3) = 0x1B; *(hex+4) = 0x00;
    *(hex+5) = 0xB6;
    Sorry I'm not making much sense, I'm pretty new to all this and other previous programming (minimal) seems to be confusing me too

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, let's try another approach: how/where are you getting these "hex values"?
    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
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Its literally test data which I then later have to convert to a type of file. They can be anything (as long as its hex) as little or as small as required to carry out an example that it works

    They are being inputted that way as thats how the lecturer deems the only correct way. Everything which i felt made sense prior to asking the lecturer for feedback seems to have been blown out the window!

    Oh but the test data has to fit into 64k of memory

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mrapoc
    They are being inputted that way as thats how the lecturer deems the only correct way.
    You did not show how the input is read.

    Generally, there are two ways to keep track of the length of a sequence: you either explicitly keep track of the length by having a variable to store the length, or you designate a special value to mark the end of the sequence. In this case, there are no special values, so there must be a variable that keeps track of the length of the sequence, e.g., this variable is modified when the input is read.

    With this variable, looping over the hex values is easy: start from the beginning, then loop until you have reached the position denoted by the length.
    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

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This question is like: I'm driving along the road, how do I know when I've reached my destination?
    So we ask: What is your destimation?
    And you answer: I'll know it when I see it.
    This is why we haven't been able to help you yet.

    Just show more code. Code speaks for itself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Ok, would probably be the easiest option. Thanks

    Code:
    /***********************************************************************/
    /*Inclusions*/
    /***********************************************************************/
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    /***********************************************************************/
    /*Indentifiers*/
    /***********************************************************************/
    
    
    unsigned char* testData(unsigned char *hex);
    
    
    /***********************************************************************/
    /*Main program*/
    /***********************************************************************/
    
    
    int main (void)
    {
    	/***********************************************************************/
    	/*Declarations*/
    	/***********************************************************************/
    
    
    	int i;
    	int x = 0;
    	unsigned char *hex = NULL;
    	hex = (unsigned char *)malloc(0x4466446);	
    
    
    	/*Create test data in memory*/
    	testData(hex);
    
    
    	/*Display menu*/
    
    
    	printf("Please make a choice: \n");
    
    
    	/*Choices*/
    	printf("1. Display contents of memory\n");
    	printf("2. Clear location of memory\n");
    	printf("3. Display location of memory\n");
    
    
    	/*Choice made*/
    	scanf("%d", &i);
    	switch(i) {
    		
    		/*Display test data*/
    	case 1:      
    		fflush(stdin);
    
    
    		for (x = 0; x <=10; x++)
    		{
    			printf("\nThe Value of hex %x is: %i\n",*(hex+x), *(hex+x));
    			printf("\nThe value of X is: %i\n", x);
    			getchar();
    		}
    		
    		getchar();
    		printf("\nPress any key to continue\n");
    		break;
    
    
    		/*Clear memory allocation*/
    	case 2:      
    		fflush(stdin);
    		printf("Clearing memory...\n");
    		hex = NULL;
    		printf("Memory cleared!\n");
    		getchar();
    		printf("\nPress any key to continue\n");
    		break;
    
    
    		/*Show memory start and end*/
    	case 3:      
    		fflush(stdin);
    		printf("Start of memory block: %p\n", hex);   
    		printf("End of memory block: %p\n", hex + 65536 - 1);
    		getchar();
    		printf("\nPress any key to continue\n");
    		break;
    
    
    		/*Incorrect entries*/
    	default:
    		printf("Incorrect option");
    	}
    	/*Clean up*/
    	printf("\nCleaning up...\n");
    	free(hex);
    	return 0;
    }
    
    
    
    
    /***********************************************************************/
    /*Setup test data*/
    /***********************************************************************/
    unsigned char* testData(unsigned char *hex)
    {
    
    
    	/*Allocate memory*/
    	if (hex == NULL)
    	{
    		printf("Cannot allocate memory \n\r");
    		getchar();
    		exit(1);
    	}
    
    
    	/*Store data*/
    	*hex = 0x0F; 
    	*(hex+1) = 0x11;
    	*(hex+2) = 0xFF;
    	*(hex+3) = 0x1B;
    	*(hex+4) = 0x00;
    	*(hex+5) = 0xB6;
    		
    	/*Return data pointer address to main*/
    	return hex;
    
    
    }

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    unsigned char* testData(unsigned char *hex)
    You're passing an unsigned char pointer "hex" to the function, then returning the same pointer from the function. However, the value of this pointer is not modified, so there is no sense in returning it.

    Instead, consider having this function return an integer - an integer which represents the maximum offset of the "hex" pointer during initialization (i.e. in your example, this integer would end up with a value of 5). This way, you program can support variabled sized initializations of "hex".

    Then simply use this return value as the criteria to exit the printing statement.

    Code:
    int testData(unsigned char *hex);
    // ...
    dataMax = testData(hex);
    // ...
    for (x = 0; x <=dataMax; x++)
    {
        printf("\nThe Value of hex %x is: %i\n",*(hex+x), *(hex+x));
        printf("\nThe value of X is: %i\n", x);
        getchar();
    }

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    hex = (unsigned char *)malloc(0x4466446);
    No need for casting in C

    Code:
    fflush(stdin);
    Undefined behavior

    Code:
    /*Clear memory allocation*/
    hex = NULL;
    printf("Memory cleared!\n");
    Setting a pointer to NULL isn't clearing the memory. You have to call free().

    Code:
    printf("Start of memory block: %p\n", hex);   
    printf("End of memory block: %p\n", hex + 65536 - 1);
    You have allocated 71722054 bytes of memory (0x4466446) thus hex + 65536 - 1 isn't the end of your memory block.

    Code:
    free(hex);
    If you have "cleared the memory" before (option 2), you have lost any reference to the memory block and free() isn't able to free the block.

    Code:
    /*Store data*/
    *hex = 0x0F; 
    *(hex+1) = 0x11;
    *(hex+2) = 0xFF;
    *(hex+3) = 0x1B;
    *(hex+4) = 0x00;
    *(hex+5) = 0xB6;
    You hardcode the length of your data so there is no other way to determine the end than also hardcode the length into your for-loop:
    Code:
    for (x = 0; x < 6; x++){
    
        printf("\nThe Value of hex %x is: %i\n",*(hex+x), *(hex+x));
    
        printf("\nThe value of X is: %i\n", x);
    
        getchar();
    
    }
    Bye, Andreas

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Thanks for the feedback all

    I'll take what you've given me and apply cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. End Loop When Empty String is Inputted
    By skmightymouse in forum C Programming
    Replies: 6
    Last Post: 05-03-2012, 11:43 AM
  2. Pointer becomes NULL inside loop
    By nepper271 in forum C Programming
    Replies: 1
    Last Post: 06-06-2010, 11:13 PM
  3. String.empty v.s. string.null
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 06-19-2008, 08:22 AM
  4. How to check if char* is empty or null??
    By earnshaw in forum C Programming
    Replies: 12
    Last Post: 12-15-2004, 07:13 AM
  5. symbol "NULL" not found????
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-23-2002, 04:39 AM