-4

I want to convert lowercase letters to uppercase letters through pointers. The code below doesn't work, it throws some error.

#include<stdio.h>

int main()
{
    char somearray[10]; char mask;
    mask = 1 << 5;
    mask = ~mask;
    char *someptr;
    gets(somearray);
    puts(somearray);
    someptr =&somearray[0];

    while (*someptr != '\n')
    {
        *someptr = *someptr & mask ;

        someptr++;
    }
    printf("%s",someptr);
    return 0;
} 

got an ERROR:

not able to compile , if compiled runtime error

Even the below code doesnt work:

#include <stdio.h>

int main()
{
    char somearray[10];
    char mask;
    char *someptr;

    mask = 1 << 5;
    mask = ~mask;

    fgets( somearray, sizeof(somearray), stdin ); /* gets() is suspect: see other reactions */
    puts(somearray);
    for ( someptr = somearray; *someptr != '\0';someptr++)
    {
        *someptr &= mask ;
    }

    printf("%s",someptr);



    return 0;
}

input : abcd output: abcd., expected : ABCD

danny
  • 1,587
  • 2
  • 12
  • 12

3 Answers3

3

gets() does not store the newline character in the array so the loop will be going beyond the end of the array and be accessing and modifying memory it is not supposed to. Use scanf() and limit the number of characters read to prevent buffer overrun (if really C++ use std::getline() and std::string):

scanf("%9s", somearray);

and terminate the loop at the first null character:

while (*someptr != '\0')

You will want to pass somearray to the printf() after the loop, as someptr will be pointing to the end of somearray and would just print an empty string.


Worth reading: warning:gets function is dangerous

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

some pointers:

use fgets to read from keybaord instead, that way you can specify a max size of the string that does not exceed your buffer

e.g. fgets( somearray, sizeof(somearray), stdin );

use the standard function tolower(int c) instead

e.g. *someptr = tolower(*someptr);

in your while loop check for end of string as well i.e. while (someptr && someptr!='\n') that way they way your loop handles both strings returned from fgets and scanf

AndersK
  • 35,813
  • 6
  • 60
  • 86
-1

I cleaned up the code a bit, but I still don't understand the intention of it.

#include <stdio.h>

int main()
{
    char somearray[10]; 
    char mask;
    char *someptr;

    mask = 1 << 5;
    mask = ~mask;

    gets(somearray); /* gets() is suspect: see other reactions */
    puts(somearray);
    for ( someptr = somearray; *someptr != '\n';someptr++) 
    {
        *someptr &= mask ;
    }

    printf("%s",someptr); /* must point to '\n', otherwise we would 
                          ** not get here BTW: gets() removes the \n 
                          ** and replaces it by a \0 so we wont get here
                          */
    return 0;
}

BTW: if you want to show your intentions to the human reader: don't use a variable when a literal would suffice:

*someptr &= ~(0x20);

But, even better:

 *someptr = toupper (*someptr);
wildplasser
  • 43,142
  • 8
  • 66
  • 109