Thread: Removing an array in an element?

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    37

    Removing an array in an element?

    Hi, i was wondering if there is a way to remove the element of an array. For example, in the following code i am using the one array to temporarily store multiple sets of data. The problem is that if the previous string was longer, it appends the current string. Each string is separated by commas.
    Code:
    for (x=0,j=0;x<i;++x) {
    		
    		if (line[x]!=',') {
    			tmpNum[j]=line[x];
    			++j;
    		}else {
    			printf("%s\n",tmpNum);
    			j=0;              //this is where i "reset the array"
    		}
    }
    so if i had the string "123", and the next string was "ab", the resulting output would be "ab3". Is there any way for me to remove the trailing characters? I know i can use a for loop and the length of each string to omit the unwanted characters but that's not my question.
    Last edited by bluej322; 02-19-2011 at 04:32 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is happening because you aren't nul terminating your strings... In C every string ends with a null ( 0 ) that tells the library functions where to stop... End Of String. When writing your own routines for string manipulation you must insert the null from your code.

    If the strings were correctly null terminated you would not have the problem...

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    37
    wow, that deserves a facepalm. Thanks!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also... you do realize you can extract formatted data from a string using sscanf(), directly from a file using fscanf() or from the keyboard using scanf()...

    For example...
    Code:
    int a, b, c;
    char test[] = "123,654,901";
    
    sscanf(test,"%d,%d,%d",&a,&b,&c);
    ... will fectch the values of a b and c from a comma delimited string.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    You can use the bzero function to nullify a string, then when you write to the string again, you won't have any leftover data.

    man page bzero section 3
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  2. Segfault when removing element from list
    By jmd15 in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2007, 12:25 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM