Thread: why i dong get accsesing null memory error stuff..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i turned into string and it still working??
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	char str1[3]={'a','b','\0'};
    	 
    	char* str=str1;
    	*(str)='h';
    		return 0;
    }

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by transgalactic2 View Post
    laser light said:
    "pointer points to the first char of a string literal, and string literals may not be modified."
    so this code works because its not ending with \0
    An array of characters is not a string literal.

    Neither is this:

    Code:
    char str[] = "foo";
    In that case, "foo" itself is a literal, but it will be COPIED into the str[] array. So it would be writable.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i made it a string literal
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	char str1[3]="ab";
    	 
    	char* str=str1;
    	*(str)='h';
    		return 0;
    }
    its working
    ??

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    so this code works because its not ending with \0
    No, it works because you are dealing with an array whose contents can be modified, not a string literal whose contents cannot be modified.

    This is correct:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char str[] = "ab";
        char *p = str;
        *p = 'b';
        printf("%s\n", str);
        return 0;
    }
    This is wrong:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *str = "ab";
        char *p = str;
        *p = 'b';
        printf("%s\n", str);
        return 0;
    }
    Spot the difference.
    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

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by transgalactic2 View Post
    i made it a string literal
    You didn't. You declared an array, not a "char *". As I already said, doing it this way causes the string to be copied into the array. The literal is the "ab" in the code, not the array. Had you declared a char *, you would be pointing at this literal, and could not modify it, but that's not what you did.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    hooooray i got my error
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	 
    	 
    	char* str="ab";
    	*(str)='h';
    		return 0;
    }
    it weird because by the definitions that i know
    Code:
    char* str="ab";
    equals

    Code:
    char str1[3]={'a','b','\0'};
    char* str=str1;
    so i cant see why one is working and the other doesnt
    ??

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so
    this is a literal
    Code:
    char* str="ab";
    and we cant change any of it cells
    thanks
    the other stuff that i showed are arrays of chars
    correct??

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by transgalactic2 View Post
    by the definitions that i know
    start learning correct definition in this case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    this is a literal
    This is a string literal: "ab"
    It is an array of three chars, including the null terminator.

    Quote Originally Posted by transgalactic2
    the other stuff that i showed are arrays of chars
    Arrays of chars are always involved where strings are involved.
    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

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you say that they both are arrays of chars.

    so what is the difference between
    Code:
    char str[] = "ab";
    and
    char* str = "ab";
    ??
    why the first is allowed and the last not
    except the fact that the first is not a literal ??
    what happens in the compiler that allowes it?

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    so what is the difference between
    The difference is that in the former, str is an array of chars that is a copy of the string literal "ab", but in the latter, str is a pointer to the first char of the string literal.

    This difference is critical when you attempt to change the contents of str, since with the former you safely change the elements of an array under your control, but for the latter you attempt to change a string literal that is stored in an area not under your control.
    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

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by transgalactic2 View Post
    you say that they both are arrays of chars.

    so what is the difference between
    Code:
    char str[] = "ab";
    and
    char* str = "ab";
    ??
    why the first is allowed and the last not
    except the fact that the first is not a literal ??
    what happens in the compiler that allowes it?
    Do you know difference of the array and pointer?

    first is array of 3 chars that is initialized with 'a','b',0

    and because it is array - you can modify each element of it

    the second is just a pointer that points to some external memory, which could be read-only (it is compiler dependent)

    you can modify your pointer but you have no right tomodify contents of the external memory
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    aaahh the memory is Read only
    so in this case char* str = "ab";
    it changes the memory

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  3. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 01:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM