[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Help with C Language

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 16
Thread images: 1

File: GDDAM POINTERZ.png (32KB, 306x382px) Image search: [Google]
GDDAM POINTERZ.png
32KB, 306x382px
Hey everyone, I need some help wth C programming.

I'm a total noob on coding, so I go a little stuck with pointers. In the image is the code of the lesson I'm in, and I just can't get why the variables s1, s2 and result or the function longer need to be pointers. And what's the magic on declaring string1 and string2 as pointers and assigning them the text value right away?

I found some nice explanation on pointers searching on the web, but I can't understand why those things need to happen like this in on this particular code. I'd take another (and lengthier and noobbier) approach for the same output. So, if you computer geniuses out there feel like helping me improving, I'd feel really glad.

Thanks in advance.
>>
>>180440
The functions in string.h take a pointer to a string as an argument.

>And what's the magic on declaring string1 and string2 as pointers and assigning them the text value right away?

I'm not sure what you mean by this. The compiler adds the null terminator automatically for a quoted string constant though, if that's what you mean. You can initialize any variable at the same time you declare it.
>>
>>180455

Thanks, I'm not sure I got the string.h part, but at least that gives me a clue on which topic to delve into next.

And concerning string1 and string2, I thought you couldn't do that because with the * operator they would only count as a pointers and their contents would be the memory addresses of the variables they would point to, so you couldn't assign them any values other than those.
>>
>>180440
>I'm a total noob on coding, so I go a little stuck with pointers. In the image is the code of the lesson I'm in, and I just can't get why the variables s1, s2 and result or the function longer need to be pointers.
C doesn't have a string type *at all*, so if they were char literals, you could only have one letter.

By making them pointers, you can keep adding 1 to the pointer and reading the next char, and that way you get a string and not just a single char.

But how do you know when to stop? By convention you check to see if the char is zero, and if it is, that's the end of the string.

What if the zero isn't there? Why you've just discovered C's greatest feature: buffer overruns with remote-execution potential! You can add a buffer-overrun vulnerability anywhere you use a pointer! And because C allows arbitrary pointer arithmetic, finding buffer overruns reduces to the Halting Problem and is provably unsolvable in the general case!
>>
>>180464
>You can add a buffer-overrun vulnerability anywhere you use a pointer!
Maybe if you're retarded.
>>
>>180440
>what's the magic on declaring string1 and string2 as pointers and assigning them the text value right away?
>>180440
>And what's the magic on declaring string1 and string2 as pointers and assigning them the text value right away?
It's shorthand for

const char[] a = "hello";
// which in turn is shorthand for
// compiler, please
// add "hello" to the constant table in the executable
// not forgetting to terminate it with a zero
// and set the address of a[] to the address of wherever it was you allocated "hello"
char *string1 = a;

When you assign an array to a pointer, the pointer automatically takes the base address of the array.

Arrays and pointers are pretty-much just different ways of writing the same thing. The only major difference is that &(p+1) is one byte after p, and a[1] is sizeof(a) bytes after a[].
>>
>>180471
So everyone involved in:
Chrome
Firefox
Flash
iOS
Linux
Android
OpenSSH
Windows

just needed to not be "retarded".

Got it.
>>
Guys, OP here. Thanks so much.

You've taught me more than almost three hours of online lessons on Lynda.com (if that makes me sound less retarded, I know there are free online lessons anywhere else, but my employers are paying the monthly fee, so).

It is beautiful to see people eager to help each other, and it's a shame that it happens so rarely. Keep being awesome.
>>
>>180463
String.h is a library of functions involving strings (or really, pointers to strings of bytes terminated by a null).

The way that strings in C work is that you start with the data at the pointer address and keep pulling sequential bytes until you hit a null character. Strings can be of various lengths. If you weren't using a pointer, they'd have to be arrays of a known, fixed size.

If you want to change the contents of a string that a pointer is pointing to, you can do one of the following:

1. Change the contents at the address the pointer is pointing to.

2. Allocate memory at a different address, set the values there (and subsequent addresses), and then change the address the pointer is pointing to to this new location.
>>
>>180440
>I'm a total noob on coding
>C Language

Why aren't you using C++?

>I just can't get why the variables s1, s2 and result or the function longer need to be pointers.

Because the names of arrays of characters are pointers.
>char a[12]="Hello world";
"a" is just a pointer to "a[0]"

>And what's the magic on declaring string1 and string2 as pointers and assigning them the text value right away?

You're declaring a local char array, initializing it with the provided string, and setting string1/2 to point to it. You could type:
>2;
>"Oh no!";
and that will be valid C/C++ code but you have no way of remembering and accessing it if you don't store the value or address somewhere.

>but I can't understand why those things need to happen like this in on this particular code
>need

It doesn't have to. You could have just written:

result = strlen(s1) > strlen(s2) ? s1 : s2;

or avoided using string.h and did:

for(int i=0;;++i){
if(s1[i]=='\0')
return s1;

else if(s2[i]=='\0')
return s2;
}

>I found some nice explanation on pointers searching on the web

Don't just use short online tutorials to learn how to code. Get a book and read it. Otherwise your knowledge is going to have a ton of gaps and your code will stink.
>>
>>180588

> Why aren't you using C++?

My engineering friends told me that learning C as my first language would give me a solid base for any other language since most are based on it.

> Get a book and read it.

Thanks! Any specific recommendation?
>>
>>180728
K&R
>>
>>180728
They're fucking with you.

Starting with C is like learning to drive in a Formula 3.

To learn curly-braces syntax, start out on Python, Java, JS or PHP*. That way you also get exposure to modern concepts like OO and Functional, and don't have to deal with shit like your language not even having native strings.

C is on the slow slide to obscurity: it used to be what everything was written in, but now it isn't: boring business software is C++, .NET, or an HTML5 framework; mobile is (thanks to Android) Java; Apple is Objective C; games use Lua; and the only place you really see C now is in Arduinos, and OS and game-engine programming.

If you want to learn C later, it's exactly like Java, except you don't get to have objects, and you have to manage your own memory.

Everything you need to know about memory management, you'll learn by writing a compiler. "Modern compiler implementation in X" ("The Tiger Book") by Appel will sort you right out with that. Pick a language you don't already know for X, and you can kill two birds with one stone. There is a C version, but learning ML will make you a better programmer.

* no-one should ever start on PHP; I mention it only because it's C-like and widely available.
>>
>>181198
OP, DON'T TAKE THIS ADVICE
seriously continue learning C, you're on the right path
also do learn some computer architecture and assembly if possible
also please learn some algebraic structures before OOP, that makes it extremely comfy to learn
>>
>>181198
>To learn curly-braces syntax, start out on Python

wtf
>>
>>181354
Indenting is semantically identical to curly braces, and parses to the exact same abstract syntax tree.

Your programming assignment for today is to write something that converts code from indentation-style to curly-braces style. This can be done with a pushdown automaton, because you don't actually need to parse or understand the code to reformat it.

Bonus points for inserting comments so it can be roundtripped preserving whitespace.

>>181296
Yes, you should totally learn some "algebraic structures".
Thread posts: 16
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.