assignment of pointer...

This is a discussion on assignment of pointer... within the C Programming forums, part of the General Programming Boards category; Hi , I am unable to debug what is going with this program of reversing a string. I had another ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    Question assignment of pointer...

    Hi ,

    I am unable to debug what is going with this program of reversing a string.

    I had another doubt that is the assignment
    *a = *p
    valid because when i try to debug the program it gives error at this line in the program below that access violation writing at location whatever address is contained in a.


    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void xstrrev(char *);
    
    int main(){
    
    	int i;
    	static char *s[] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    
    	for(i=0;i<3;i++){
    		xstrrev(s[i]);
    		printf("\n\r%s", s[i]);
    	}
    	return 0;
    }
    
    
    void xstrrev(char *a){
    
    	int len,j;
    	char *p, temp1, temp2;
    
    	//printf("\n\r%c %d", *a,a);
    	len = strlen(a);
    	//printf("\n\r%d %d", len, len/2);
    	p = a + len -1;
    	//printf("\n\r%d %c", p, *p);
    	for(j = 1;j<= len/2;j++){
    	
    		temp1 = *a;
    		temp2 = *p;
    		printf("\n\r %c %c",*p, *a);
    		*a = *p;
    		printf("\n\r1\t%c\t%c",*a,*p);
                    // *p = temp;
    		//printf("\n\r2\t%c\t%c", *p, temp);
                    ++a;
    		printf("\n\r3");
                    --p;
    		printf("\n\r4");
            }
    }
    Last edited by roaan; 07-08-2009 at 04:33 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can't modify string literals, you've been told this before.

    i.e, your "s[]" array contents cannot be legally modified in most implementations. Thus you should be making it constant. As for the other parts, "What the!?".

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    You are getting an access violation because your strings are stored in a non-writable part of memory. You need to actually make an array of C strings instead of an array of pointers to C strings. That way your C strings will be copied into the array.
    Try this:
    Code:
    	char s[3][80] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    For you reversal routine remember to uncomment your *p = temp and change it to *p = temp1. You have the right idea that you need to store *a before overwriting it with *p.

    EDIT:
    Also, when testing, use data that is easy to check, and make sure you test the strange "corner" cases. For example:

    Code:
    	char s[5][80] = {
    		"1234",
    		"123",
    		"12",
    		"1",
    		""
    	};
    Test with a 0 length string, test with a length of 1, test with an odd length, test with an even length. If it works for these, it will work for others.


    Question, can someone provide a more elegant syntax for the char s array?
    Last edited by Zlatko; 07-08-2009 at 05:09 AM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    a printf("\n") is sufficient to generate a newline.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    Hi ,

    I am unable to debug what is going with this program of reversing a string.

    I had another doubt that is the assignment
    *a = *p
    valid because when i try to debug the program it gives error at this line in the program below that access violation writing at location whatever address is contained in a.
    I've explained this question here. After going through my post #20 if you still have any problem, then post the updated code and your query.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    Question

    I am really in distress now after having spent so much time. Okay i tried using this modified approach but still it does not work . I don't understand what i am doing wrong now. Please ignore the printf statements as i was just trying to understand what it is doing at each step.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void xstrrev(char *);
    
    int main(){
    
    	int i;
    	static char *s[] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    
    
    	for(i=0;i<3;i++){
    
    		printf("\n\rBefore reversal%s", s[i]);
    		xstrrev(s[i]);
    	}
    	return 0;
    }
    
    void xstrrev(char *a){
    
    	int len,i,j;
    	char *p, temp1, temp2;
    
    	//printf("\n\r%c %d", *a,a);
    	len = strlen(a);
    	printf("\n\r%d %d", len, len/2);
    	p = malloc(len+2);
    	printf("\n %d", p);
    		
    	//printf("\n\r%d %c", p, *p);
    	while(*a != '\0'){
    		printf("\n %c", *a);
    		a++;
    	}
    	for(i = len;i>=0;i--){
    		
    		*p = *a;
    		printf("\n %c", *p);
    		printf("\n %d", p);
    		//printf("\n %c", *a);
    		p++;
    		a--;
    	}
    
    	*p = '\0';
    	printf("\n%c", *p);
    	printf("\n %d", p);
    	p = p - (len+1);
    	printf("\n %d", p);
    	printf("\n\rAfter reversal%s", p);
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This can never work:
    Code:
    static char *s[] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    The contents of s are constant and can never change. If you want to change them, you must use something else. See Zlatko's earlier post for an alternative.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,609
    You are just using an array of string literals.
    And as has been pointed out to you, string literals cannot be changed. End of story.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    No but now i am not trying to reverse the string literal now i am trying to reverse it in a new string p (I am copying the reverse characters in p with that pointed to in the original string so i am not at all modifying the original string literal)

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You start with the first char in the source string and go back...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    OK, take a deep breath. It's going to be OK. I see that you've changed your approach, and are copying the string a into the allocated memory p but backwards. We can work with that.

    Look at this code
    Code:
    	while(*a != '\0'){
    		a++;
    	}
    You've incremented a until it points to the '\0'
    Then you're putting the '\0' into the first location pointed at by p. You don't want your reversed string to start with a '\0'. Thats the first problem. Adjust 'a' before going on.

    Then, how many characters do you want to copy. len, right?
    Code:
    	for(i = len;i>=0;i--){
    
    		*p = *a;
    		p++;
    		a--;
    	}
    How many is the above loop copying? It's copying len + 1 because its going from len through 0.
    Fix that.

    Then you have
    Code:
    	*p = '\0';
    	p = p - (len+1);
    You've null terminated p, thats good. You want to move p back to the beginning of the string, so you want to move it back len characters.
    Imagine the string abc in memory. It lools like this
    'a' 'b' 'c' '\0'
    P is pointing to the '\0'. Len is 3. Move p back 1, it points to c, move p back 2, it points to b, move p back 3 (len), it points to a, where you want it.
    Fix that, and you're done.
    Last edited by Zlatko; 07-08-2009 at 09:17 AM.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    HURRAY :-)!!!!!!!!!!!!!!!!!!!!!!
    Its working now I am really thankful to you guys for helping me out.
    And with that last post i have learnt how to debug using small example first and then using it on bigger things later.

    Thanks !!!!!!!!!!!!!!!!!!!!!!!

  13. #13
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Tonight, I'm having a drink for you!

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    This can never work:
    Code:
    static char *s[] = {
    		"To err is human...",
    		"But to really mess things up...",
    		"One needs to know C!!"
    	};
    The contents of s are constant and can never change. If you want to change them, you must use something else. See Zlatko's earlier post for an alternative.
    Why not? We can assign a pointer a chunk of memory using malloc and then using strcpy copy the original string to the pointer and then it's no longer a "constant". I've done this using this approach.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,609
    Tabstop refers to that you can never change s. If you want to change s, you must use something else.
    Of course you can copy it elsewhere and modify it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Replies: 3
    Last Post: 06-19-2004, 10:24 AM
  4. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 10:08 PM
  5. When do we get Null Pointer Assignment
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 01:48 AM

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