+ 6

strupr in C

I was using a string manipution function that uppercase the provided string //strupr(); But for unknown reasons I can't use pointer based string 🤔 like char *="all clear"; however char array works like char[10]="all clear"; And when I combine both the methods it shows no output on sololearn ide. https://code.sololearn.com/cz88Y6DaHLEI/?ref=app Please help me to understand this . I used cxxdroid and c4droid also they give errors by both the methods (char * and char [10] ) saying that strupr not defined 🤔 Subquestions : Well in C we don't have string data type so we use char array or char pointers . So... should I even use char pointers for strings ? Because this is deprecated in C++ as per my knowledge. Edit: < 12 sep 2019 code link removed adding code below > #include<stdio.h> #include<string.h> int main() { char s1[10]="All clear"; printf("%s",strupr(s1)); /* char *s2="All clear"; printf("%s",strupr(s2)); */ //uncomment above statements . return 0; }

19th Aug 2019, 10:31 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
5 ответов
+ 4
devanille very good solution 👌 Like it. //just don't like that I'll need to use loops. Pointer based strings work with other standard string functions . But strupr isn't standard one (lately understood it's actually deprecated) Thank you so much.
19th Aug 2019, 12:27 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
I'm not to sure, but I think it has something to do with the fact that string literals are immutable. Maybe, the strupr function modifies the string directly, but since a string literal is immutable, doing so will cause a segmentation error (no output in the playground). If I'm not mistaken, strupr is part of POSIX library, so not all compilers can support it. And, it's probably better to use string class in C++, but personally, I still use something like C-style string (char pointer and other things) because it's simpler for me, and it has become some sort of a habit
19th Aug 2019, 11:21 AM
Agent_I
Agent_I - avatar
+ 3
Just a bit of advice, if you assign a string literal to a char pointer, like this one char* str = "Some_String" It's better (almost mandatory) to use const specifier const char* str = "Some_String" Like I said, something like that is actually immutable, some compilers will allow the absence of const specifier, but you will get segmentation fault anyway if you try to modify it. With the const specifier, you will get a compilation error instead of runtime error if you try to modify it or pass it to a function that will modify it
19th Aug 2019, 1:00 PM
Agent_I
Agent_I - avatar