Thread: storage beyond NULL

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    13

    storage beyond NULL

    Hi guys,

    Im trying to store characters beyond my '\0' character in an array of char

    Code:
    char myvar[256] = {};
    Im filling up character by character trying to get something like:

    H | E | L | L | O | \0 | M | O | R | E | \0

    but i get segmentation fault all the time...Is there a way to work this out?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you should post ALL of your code?

    --
    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
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    sorry I think I blamed the wrong function...here is the code

    Code:
    //this function works great it adds the second string after the 
    //null character in base propperly
    int build_stream(char * base , char * add)
    {	
    	int i;
    	int len = (strlen(base) + strlen(add)) + 1;
    	
    	for (i = strlen(base) + 1; i < len ; i++)
    	{
    		base[i] = *add++;
    	}
    	
    	base[len] = '\0';
    	
    	return len;
    }
    
    // here is where the problem is, this is a function I was making 
    //to get back the added string after the null into a variable dest.
    
    void getit(char * dest, char * text)
    {
    	int i, j;
    	int limit = 3;
    	for (i = limit; *text != '\0'; i++)
    	{
    		dest[i-limit] = text[i];
    		text++;
    	}
    	dest[i - limit] = '\0';
    }
    
    int main()
    { 
    	char myvar[256] = "OK";
    	
    	char *t    = "HELLO";
    	int j, i;
    	char user[15] = {};  // this variable will be used to take 'HELLO' back
    	
    	j = build_stream(myvar, t); // after this call myvar will be holding OKnullHELLOnull
    	getit(user, myvar); //this call doesnt work properly
    	
    	printf("myvar: %s\n", myvar); // outputs HELLO since printf works till the first null
    	printf("user : %s\n", user); // outputs HL for some reason and no HELLO
    
    
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    IMO getit() has too many moving parts, so either increment the array index or the pointer but not both.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But your getit works just like strcpy. It travels the string until it finds \0, then stops.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The best way to make multistring values (as per REG_MULTI_SZ) is to concatenate strings together using a '\n' in place of the '\0' you want between the strings (and at the end) and then replace all '\n' characters with '\0' characters once you've built the whole string.

    If that isn't exactly what you're doing this for, then I think you should still adapt the idea to your situation anyway.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  3. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  4. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  5. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM