Like Tree1Likes
  • 1 Post By oogabooga

C pointer assignment going awry

This is a discussion on C pointer assignment going awry within the C Programming forums, part of the General Programming Boards category; Hello all, I'm trying to create a simple routine where I transform the individual characters of a character string to ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    C pointer assignment going awry

    Hello all,

    I'm trying to create a simple routine where I transform the individual characters of a character string to uppercase. I'm trying to figure out why I can dereference individual elements, but can't assign to them. I must be committing some fundamental mistake, but my book pretty much has the same logic I'm using. Any hints? Thanks in advance, as usual!

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    int main(void){
    	char *c = "hello!";
    	int i;
    	for( i = 0; *(c+i) != '\0'; i++){
    				if(islower(*(c+i))){
    				*(c+i) = toupper(*(c+i));  //boom!  Why?
    				}
    	}
    
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're trying to assign to a constant string "hello!" which c is pointing to. Instead, set aside some independent storage for c that is initialized with the string "hello!" like this:
    Code:
    char c[50] = "hello!";

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,281
    c is just a pointer, pointing to what appears to be unwritable memory. See if this helps: Question 6.2

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Sho' nuff! The below works, thank you!

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<string.h>
    
    int main(void){
    	char c1[888] = "hello!";
    	char *c = c1;
    	int i;
    	for( i = 0; *(c+i) != '\0'; i++){
    
    		if(islower(*(c+i))){
    				*(c+i) = toupper(*(c+i));
    				}
    	}
    	printf("%s",c);
    
    }
    Last edited by patricio2626; 02-22-2012 at 03:53 PM. Reason: Edit - bad ctrl+c

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only difference between your code in post#4 and post#1 is the apparently useless fflush(stdout) !?
    GReaper likes this.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Yep, apparent improper use of ctrl + c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer assignment
    By shikhardeep in forum C++ Programming
    Replies: 5
    Last Post: 09-04-2011, 07:02 AM
  2. Iterations Gone Awry
    By DonFord81 in forum C Programming
    Replies: 5
    Last Post: 09-29-2009, 12:53 PM
  3. assignment of pointer...
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-08-2009, 11:07 AM
  4. Pointer assignment
    By MAx12345 in forum C Programming
    Replies: 16
    Last Post: 07-10-2008, 10:42 PM
  5. Assignment [pointer]
    By aotomato in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2004, 03:31 AM

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