Assigning integer to char array.

This is a discussion on Assigning integer to char array. within the C Programming forums, part of the General Programming Boards category; Hi I am trying to write a function whihc will convert lower case string to upper case string. I have ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    50

    Assigning integer to char array.

    Hi

    I am trying to write a function whihc will convert lower case string to upper case string. I have written the function as below

    Code:
    
    char* toUpper(char* str)
    {
            int i = 0;
        char *upperStr=str;
    
           int d=0;
            for(i = 0;i < strlen(str); i++)
            {d = toupper(str[i]);
            upperStr[i]=d;
            }
            upperStr[i] = '\0';
            return upperStr;
    }
    My code is is failing at the statement upperStr[i]=d.

    Can anyone help what could be the reson for this

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Failing how? You aren't by any chance trying to modify a string literal?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    50
    While i am trying to assign integer value to the first element it is showing code dump

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Now it really looks like "trying to modify a string literal". If you call this with
    Code:
    toUpper("string");
    all you're doing is lighting a stick of dynamite and waiting for it to go boom.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why you should never try to return an array from a function...
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Eh, OP is returning the original pointer (like strcpy et al), so that's not actually an issue.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Eh, OP is returning the original pointer (like strcpy et al), so that's not actually an issue.
    Ok... My bad.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CommonTater View Post
    Ok... My bad.
    Actually, it is a good point though, because I'll bet like anything that the OP thought he was making a brand new copy with fresh pristine memory, and not overwriting the original (e.g., being careful to null-terminate the "new" string). If so, then OP needs to look at malloc.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by SasDutta View Post
    My code is is failing at the statement upperStr[i]=d.
    What does "failing" mean?
    Failing to compile? Failing to execute? Failing to give the expected result? Failing to complete without crashing?

    You need to be specific. What actually happens?
    How are you calling the function?
    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. convert char array to integer number
    By droseman in forum C Programming
    Replies: 15
    Last Post: 01-19-2010, 04:10 AM
  2. Assigning digits to cells in an integer array
    By fred2028 in forum C Programming
    Replies: 1
    Last Post: 10-11-2006, 09:08 AM
  3. Converting Char Array Loop values to integer
    By azamsharp1 in forum C Programming
    Replies: 8
    Last Post: 10-27-2005, 09:13 AM
  4. char array string > integer
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-03-2004, 11:28 AM
  5. how do i convert a char array to an integer ? (c++)
    By mbh in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2002, 03:49 AM

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