Jump to content

so I'm sitting in lecture right now and have no idea what a C-string is... Treat me like I'm 5 years old and explain it please! ?

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to post
Share on other sites

19 minutes ago, fizzlesticks said:

An array of characters that ends with a null terminator ('\0').

Literally, that's it? My professor rambled for an HOUR about memory locations and crap, and it's just a character array with a terminator? Thanks. lol

*_*_*_*_*_*_*Personal Rig*_*_*_*_*_*_*

CPU: Core i7 7700k @ 5.1 GHz

GPU: EVGA GTX 1070 SSC ACX 3.0

MOBO: ASROCK Z270 Killer SLI/AC

RAM: 16 GB HyperX Fury

COOLER: NZXT Kraken X52

CASE: NZXT S340 Elite

STORAGE:  500GB NVME Samsung 960 Pro

500GB Samsung 860 EVO

4TB Seagate Barracuda (7200 RPM)

OS: Windows 10 Pro

 

Link to post
Share on other sites

2 hours ago, Wayne Geissinger said:

Literally, that's it? My professor rambled for an HOUR about memory locations and crap, and it's just a character array with a terminator? Thanks. lol

Well, it's important to know that a C string is an array. So you can do fun things like:

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char * foobar = (char *)greeting;

foobar ++;
*foobar = 'a';

And that'll change the string to "Hallo". Of course you could just do

greeting[1] = 'a';

Anyway, make sure that the string ends in a \0, otherwise if you try to print it, it'll just barf data.

 

EDIT: However, this is a dangerous kind of C string

char *string_1 = "Hello";

This is treated as a read-only string. If you attempt to modify it, you'll get a segfault.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×