Thread: strings Vs. Char pointers

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

    Question strings Vs. Char pointers

    Hello,
    I wanna illustrate the difference between a char array (a string that is) and a pointer to char using a simple function which say capitalizes all the letters of a given string.

    To that affect I have written a simple code which (hopefully) should be doing just that. I have a function which does the checkin g and the capitalizing part. The function accepts a char pointer as an argument so I pass it a char pointer once and a char array the other time.

    So I hope that the former one should give me a seg fault as i am trying to access and write back to a memory element which does not belong to the function (is it a scope issue or what??...im bedazled as of now). And yeah it does flag a seg fault for the former case.

    However when I pass the array (using the name of the array which is also a char pointer) it still flags the seg fault error!!!..why is that..

    heres the code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void set2caps(char *);//func prototype for capitalizing letters of string
    
    int main()
    
    {
        char *str = "string for pointer";
     //   char str1[] = "string for array";
    
        set2caps(str);
        set2caps(str1);
    
        puts("strings displayed in caps\n");
    
      //  printf("str in caps is &#37;s\n",str);
        printf("str1 in caps is %s\n",str);
    
        return 0;
    }
    
    void set2caps(char *str_hld)
    
    {
        unsigned int chr_cnt = 0;
        char temp;
    
        while(str_hld[chr_cnt]) {
            if (!isupper(str_hld[chr_cnt]))
                temp = toupper(str_hld[chr_cnt]);
            str_hld[chr_cnt] = temp;
            chr_cnt++;
        }
    }
    Last edited by aijazbaig1; 02-01-2008 at 01:36 PM. Reason: I asked a wrong question!!..geez

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying pointers
    By Tom Bombadil in forum C Programming
    Replies: 10
    Last Post: 05-22-2009, 01:26 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM