Is this code trivial?

Is this code trivial?

Is it just printing the string? If so it's a redundant implementation.

i'm no wizard, but couldn't you just printf it?

Yes. It is trivial.

No, printing strings is one of the hardest topics in computer science right now. If you find a way to to do, you will have proven that P=NP and launch humanity into a new era of technological progression.

...

Yes it is. It also seems invalid. Since the while loop needs a condition.

it has a condition

the existence of *ptr

the condition is the dereferenced pointer to the string.

All strings have a last character that basically tell whatever is reading it that it is over. When the while loop reaches that character then the while will become false and stop.

I think. Your perspective of this not being a statement probably comes from using non-C languages that don't have this raw power.

Its condition is the character at the memory address contained within ptr. The null character at the end of all C strings is just a short of value 0 which gets interpreted as "false" (there is no bool type in C) thereby terminated the loop when the ptr reaches the end of the string.

Oh god C-tryhards crack me up. Yeah yeah, pointers are RAW POWER dude. Shut up.

>Yeah yeah, pointers are RAW POWER dude. Shut up.

But they are and it makes sense for someone not knowledgeable of C to understand what is going on. In other languages to loop through the characters of a string you have to do so in an 'artificial' way instead of quite literally moving around the memory that contains the characters of the string.

pythonbabby detected

all the stuff you import was created by "C-tryhards"

enjoy the fruits of the tree we planted

> strings are made of one-byte symbols

Yes. In Javascript all you have to do is:

console.log("I'm just a normal string.");

Holy fuck C-tards are dumb as fuck to need 15 lines to print a character array.

printf("I'm just a normal string.\n");

first of all, yes your code is trivial aside form the fact that you're relying on the NULL pointer being present after you're string in memory to falsen your while condition. this may or may not work depending on what operating system you're on and, if the OS (if any) allows you to walk through memory not allocated to your process, there actually is a non initialised piece of memory there to represent your NULL pointer. however, this is considered bad practice as it is not in the C standard and you should really use a universally viable condition for the while loop to terminate, such as explicitly checking for the '\0' character (although even this is not necessarily the best idea in a more generic context, but here, as your string is initialised generically, it is okay.

second of all, this thread is really horrible. it's not very scientific and it has successfully all the fucking retards who do not know anything whatsoever about C, coding practice in general and are generally just know-it-alls with a superiority complex who dismiss C out of hand because "muh javascript", which is honestly the most retarded fucking thing i have ever heard in my entire life.

look, if you like python or javascript or whatever other language you're working in and you have not (yet) encountered the need to work with anything else but high level languages, good for you. the thing is, there sometimes simply is no way around C; embedded, for example, is simply so dominated by C that you COULD in theory work in a different (albeit still low level) language but you're essentially shooting yourself in the foot since all your tools and environments will be geared for C, the support from the manufacturer of the hardware will be focused on C and your colleagues will know C and have worked in C and for the most part not be very inclined to work with the latest trend language seeing as they are probably 50 yrs old and just don't care anymore.

cont'd

or high performance computing, where your choices literally are C/C++ and Fortran, so yeah, i guess you could go ahead and learn yourself some Fortran and you will be fine, but given the choice, most people working there today also opt for C/C++ and yet again - even though in this case the environment and tools etc are still largely there for Fortran, if on the way out - you're bound to C/C++ by your working environment. not even talking about the fact that the operator of any HPC facility will likely laugh in your face if you hand him your haskell program because "it runs just as fast as my C code on my machine".

so yeah, kids. you can shit all over C all you want but if you're serious about CS and interested in the parts of it beyond "muh machine learning framework" and pure fucking language theory, you will encounter C. period.

if you're one of the Java/JavaScript/Webdev/Frontend retards, you're lost anyway and honestly why the fuck are you even in CS, you should be in codecamp or whatever that shit is called.

>there actually is a non initialised piece of memory there to represent your NULL pointer

No. There is most certainly not. First, it's not a NULL pointer, it's a pointer to a null character. Second, there is an actual, allocated byte there, set to '\0'. There has to be. That is the only indication that the string is at an end; if it were uninitialized, no function would reliably print the string (e.g, printf would print the string plus a bunch of garbage.)

The line
char* string = "hello";
allocates six bytes, and sets them to the values 'h', 'e', 'l', 'l', 'o', '\0'. The zero character is, by construction, at the end of any string literal in c. Of course, if he rewrote the value at the end (e.g., *(string + 5) = '!';), *then* he'd end up printing "hello!" followed by a bunch of garbage, and possibly a SEGFAULT. But so would printf.

>if you're one of the Java/JavaScript/Webdev/Frontend retards
Typical codemonkey, if you're serious about CS you should major in math. period.

>Of course, if he rewrote the value at the end (e.g., *(string + 5) = '!';)
That's a segfault. String literals are read-only.

In C you can do

puts("I'm just a normal string.");

but programming without pointers is useless

> you're relying on the NULL pointer being present after you're string in memory

You mean a null *character*, and this is guaranteed by the C standard.

>For loop
Pretty trivial

>this is guaranteed by the C standard
No it's not.

>Create array of chars shorter than the given one
>Run strcpy
>Get segmentation fault