cognition on world of Programming (specifically in C)

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Hi guys, I'm struggling the concept of how several things aren't the same type but it's the same at the output. let me clear more, how actually to say str[5]=='\0' is the same to say str[5]==0 which they mean we arrived to the final of the string?(assuming that str is a string .. char *) and not specifically that case, I'm talking in general how multiple things with different types leading to the same output? and they are equal in meaning?! how we decide that? actually how could multiple things with different representation be the same in meaning(if there's an analogous for would be nice!!) ?!
is there a theory in math that declares although different in representation, it could lead to the same meaning?
thanks in advance.
 

MrChips

Joined Oct 2, 2009
34,919
There are many examples that could be totally confusing to the newcomer.
In time, you will learn the meaning and differences. In the meantime, you need to read the manual and learn things one at a time.
 

Thread Starter

Ryan$

Joined Dec 14, 2018
178
There are many examples that could be totally confusing to the newcomer.
In time, you will learn the meaning and differences. In the meantime, you need to read the manual and learn things one at a time.
I've already read.. but not succeeding in understanding how different "representations" leads to the same meaning? in other words the same thing..
 

WBahn

Joined Mar 31, 2012
32,944
Hi guys, I'm struggling the concept of how several things aren't the same type but it's the same at the output. let me clear more, how actually to say str[5]=='\0' is the same to say str[5]==0 which they mean we arrived to the final of the string?(assuming that str is a string .. char *) and not specifically that case, I'm talking in general how multiple things with different types leading to the same output? and they are equal in meaning?! how we decide that? actually how could multiple things with different representation be the same in meaning(if there's an analogous for would be nice!!) ?!
is there a theory in math that declares although different in representation, it could lead to the same meaning?
thanks in advance.
It's called the transitive property.

If a = b and b = c, then a = c.

If 36 inches = 3 ft and 3 ft = 1 yard, then 36 inches = 1 yard.

In your example, '\0' is an expression that evaluates to the character code having a value of 0.
 

WBahn

Joined Mar 31, 2012
32,944
I've already read.. but not succeeding in understanding how different "representations" leads to the same meaning? in other words the same thing..
If I tell you that Joe is 72 inches tall and that Fred is 6 feet tall, can you not draw from these two different representations of length that Joe and Fred are the same height?
 

MrChips

Joined Oct 2, 2009
34,919
In programming, there are different ways to express the same thing. You have to learn the rules and conventions.

x = 0;
x = 0x0;
x = '\0';

would translate to the same operation and data assignment if x is of type char.
 

bogosort

Joined Sep 24, 2011
696
is there a theory in math that declares although different in representation, it could lead to the same meaning?
For the math perspective, consider this set of numbers: { 1/2, 2/4, 4/8 }. In the context of the rationals, each is a different representation of the same meaning (i.e., the number 1/2). This idea is generalized in the notion of equivalence classes -- each member in an equivalence class has a distinct representation but represents the same thing, according to some equivalence relation. In our example, the relation is a/b = c/d when ad = bc.

Another familiar example is the congruence of two triangles in Euclidean geometry. Picture two right triangles with the same lengths and angles, then rotate one of them 45°. In the context of the plane, they are different representations of the same thing.

More generally, the concept of some mathematical object being the same, with respect to some category, as another mathematical object is called an isomorphism (which literally means "having the same shape"). If two things look different but preserve a mathematical structure or relationship, we say they are isomorphic to each other with respect to that structure. For example, in graph theory, two graphs are isomorphic if they share the same adjacency structure. They may be drawn and labeled differently, but fundamentally they are the same graph.

Math is literally filled with things that have different representations but the same meaning. (One of the holy grails of mathematics is to find how all of its wildly varying fields of discipline are fundamentally connected.) In a sense, however, this idea is reversed in computers: many things have the same representation but different meanings/interpretations. CPUs turn electrical switches on and off; that's all they can physically do. But, depending on how some of the switches are configured, another set of switches will be interpreted as an ASCII character, or an integer, or an instruction, etc. In other words, the same sequence of high-low voltages can be interpreted in vastly different ways. This is what happens when you printf a %d instead of a %c on, say, 'A': the latter shows the character, the former shows the number 65 (ASCII code for 'A').

While this is true at the level of the hardware/software interface, we typically interact with the computer at higher levels of abstraction, and the various layers of abstraction bring us back to the "different-looking but the same" paradigm. In C, an int variable and a pointer to that int variable give us two different-looking interfaces to the same thing. Opening a file looks the same as opening any other file, because fopen() -- and a huge number of other layers -- has abstracted away the differences.

There's so very much abstraction in computers that it can be difficult to realize what's really going on, but keep learning and you'll be able to mentally zoom in and out of the abstraction layers, and it'll all start making intuitive sense.
 

MrSoftware

Joined Oct 29, 2013
2,273
Memory is just numbers, memory does not have "types". But to make life easier for humans, compilers were programmed to understand "types". What actually happens behind the scenes is the compiler applies the "type" to the data that you specify, and translates that to the actual numbers to write to memory. For example, when you say this:

str[5]=='\0'

The '' tells the compiler that you're indicating a char type, so the compiler translates the char value of \0 to the actual numerical value of "0". When you say:

str[5]==0

you are telling the compiler directly that you mean the numerical value of "0".

Learn to use the memory watch on your debugger, then watch the actual memory as you write different "types" to memory. You will see the actual data written. For example, do this:

int myVar;
Now set the memory watch on your debugger to watch the memory at address &myVar. Do this:

myVar = 5;

The data at memory address &myVar will be "5". Now do this:

myVar = (int)'5';

The data at memory address &myVar will be "53". Why? Because the compiler reads takes '5' to be the char (ASCII) value of 5,and the acutal numerical value for an ASCII 5 is 53. Here is some code to play with:

Code:
#include <stdio.h>

int main()
{
    int myVar;
    myVar = (int)'5';
    printf("The integer value of character 5 is %i\n", myVar);  // Interpret data stored in myVar as int and print it
    printf("The character value of charater 5 is %c\n", myVar); // Interpret data stored in myVar as char and print it

    return 0;
}
 

djsfantasi

Joined Apr 11, 2010
9,237
In programming, typing is often enforced to avoid errors by inadvertently mixing two different values. In the old days, one could assign a floating point variable to an integer. The results were unpredictable, often resulting in a failing program.

On the other hand, if one was knowledgeable enough, you could use this behavior To your advantage.

Varying representations are useful in certain situations. A non-programming example might be illustrative. There is one me! There is one you! However, in different situations, I have been referred to as Don, Donald, Duff, Junior, DJ, Honey, and “The Great God of the System”. In different situations, I am referred to by one of these names. But in all cases, the name refers to me.
 
Top