Re-Assigning Values To Array

This is a discussion on Re-Assigning Values To Array within the C Programming forums, part of the General Programming Boards category; Hi, i'm really new to (C) programming. I'm practicing by writing a code that will take a string, and then ...

  1. #1
    Windu102
    Guest
    Hi, i'm really new to (C) programming.
    I'm practicing by writing a code that will take a string, and then send the individual words of that string, to a function to be printed. The problem is that because i override the characters in the respective elements of the array, if one of the words is shorter than the one before it, it carries across the excess letter.

    E.g "what you want" gives me :
    what you want
    what
    yout
    want

    I'm thinking i need to re assign the value 0 to my whole array (since apparently i cant re-initialize in C), problem is the loop I'm using doesn't work. When i compile it in Visual C++, i get the following message.

    "Run-Time Check Failure #2 - Stack around the variable 'textblock' was corrupted."

    You'll see the loop commented out in the second If statement in my While.

    Warning. What your about to see is a very ugly, and extremely inefficient chunk of code. Please be gentle.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(int argc, char *argv[]){
    
    char mytext[21] = {"what you want"};
    char textblock[21] = {0};
    int wordplace(char textblock[]);
    
    printf("%s\n", mytext);
    
    int i = 0;
    int j = 0;
    int f = 0;
    
    while((i<22) && (j<22) && (mytext[i] != '\0')){
    	
    	if(( mytext[i]>='a' && mytext[i]<='z' ) || ( mytext[i]>='A' && mytext[i]<='Z' )){
           textblock[j] = mytext[i];
    	}
    
    	i++;
    	j++;
    
    	if (mytext[i] == ' ' || mytext == 0 || mytext[i] == '\0'){ 
    		mytext[i] = '\0';
    		wordplace(textblock);
    		i++;
    		j = 0;
    		//while (j<22){
    	       //textblock[j] = 0;
    		   //j++;
    		}
    		
    	}
    
    }
    
    return 0;
    
    }
    
    int wordplace(char textblock[]){
    
    	printf("%s\n", textblock);
    
    	return 0;
    
    }

    Please help. Thank You.
    Last edited by Windu102; 08-21-2008 at 08:20 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		mytext[i] = '\0';
    		wordplace(textblock);
    Perhaps you should be updating textBlock[j] rathter than mytext[i] here?

    --
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By the way:
    Code:
    (i<22) && (j<22)
    will allow i or j to reach 21, which is beyond the end of a [21] array. It should be 21 not 22.

    --
    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.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You need to reset j to 0 after that commented-out while loop...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    You need to reset j to 0 after that commented-out while loop...
    Although the entire commmented out loop would be superfluous when using the code suggested in my first reply. The second reply explains why the stack block is being correupted, as it writes to location 21 of a [21] array, which is out of bounds, as a 21 item long arrat allows 0..20 as valid indices.

    --
    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
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    Although the entire commmented out loop would be superfluous when using the code suggested in my first reply. The second reply explains why the stack block is being correupted, as it writes to location 21 of a [21] array, which is out of bounds, as a 21 item long arrat allows 0..20 as valid indices.

    --
    Mats
    You may have given him "proper" way of doing it, but i just pointed out (to him) "error" in his way...(not saying it's "proper" or good)

    EDIT: actually, I'm totaly wrong (), because I thought he passed over whole stack (and not "only" 1 byte), but it's obvious he has runtime protection which detected that 1 byte overflow (i thought it was segfault...)

    EDIT2: but anyway, if he will use that loop, he should reset j to 0 (and use "j<21" of course)
    Last edited by rasta_freak; 08-21-2008 at 01:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 02:57 PM

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