Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (VII)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Wed Jun 13, 2012 10:05 am

snailface wrote:Here is a rewrite of Ex4. It encrypts, decrypts, and prints at each stage. It's a lot more compact than my first attempt and hopefully, you'll like it better. :)
Cool, but we didn't see loops yet, so it's not valid :) Also as Freddy points out, you don't free your mallocs ;)
snailface wrote:char *str=(char*)malloc(5+1); //I can't initialize as a const literal because I can't modify the string that way
Anyone said strlen()? ;)
snailface wrote:char *str=(char*)malloc(5+1); //I can't initialize as a const literal because I can't modify the string that way
strcpy(str,"Snail");
Why such complications? Isn't char* str = "Snail" easier?
snailface wrote:Can you please explain why? I've seen that (int*) typecast convention with malloc() used in several books including K&R.
Memory leaks means you're not freeing the reserved memory. It has nothing to do with casting ;)
snailface wrote:I know this is unorthodox code, but it is perfectly valid code
Yup, my bad, didn't look closer :roll:
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by Wdingdong » Wed Jun 13, 2012 12:36 pm

m0skit0 wrote: Yeah! :)
Correct!
Yeay :D Though I'll be asking more doubt on this.
FrEdDy wrote:Repeat after me: always free your memory,always free your memory...
Always free your memory, always free your memory, always free your memory.... :D
snailface wrote:Still, I should probably avoid using this weird method since it confuses and frightens people.
:lol:
Advertising
// Big thanks to people who share information !!!

Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by Wdingdong » Wed Jun 20, 2012 6:48 am

Till now I was thinking that pointers are abstract identities which points to some memory locations. But, now I came to know that pointers are also variables which stores address of another variable. Is it right? Also, can there be a pointer to point memory location of another pointer? Something like this:

Code: Select all

int x;
int* a;
int** b;
a = &x;
b = a;
// Big thanks to people who share information !!!

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: [Tutorial] Introduction to programming using C (VII)

Post by JJS » Wed Jun 20, 2012 7:21 am

Wdingdong wrote:pointers are (also) variables which stores address of another variable
This is exactly what a pointer variable is. Edit: It can also point to functions and const values, not only "variables" in the strictest sense.
Wdingdong wrote:Also, can there be a pointer to point memory location of another pointer?
Of course. But in your example you probably mean the last line to read

Code: Select all

b = &a;
because otherwise dereferencing it twice to get the value that is ultimately pointed to you would dereference x as a pointer which is wrong.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Wed Jun 20, 2012 10:38 am

Wdingdong wrote:now I came to know that pointers are also variables which stores address of another variable. Is it right?
As JJS points out, yes, you're correct. Pointers are variables that hold a memory address (this not necesssarily points to a variable, not even to data, as JJS also points out). This is why you're able to do operations with them and even print them using printf() like any other variable type.
Wdingdong wrote:Also, can there be a pointer to point memory location of another pointer?
Yes, and just to clarify: and a pointer to another pointer that points to another pointer, and a pointer to another pointer that points to another pointer that points to another pointer... And so on. For example, an array of character strings would be a pointer to a pointer, e.g.:

Code: Select all

char* lotsofstrings[20];
char** decaying = lotsofstrings;
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Fri Jun 22, 2012 2:25 am

Using only pointers: declare an array of signed integers and assing it a size of 50 integers.
Same as the last exercise but also assign positions 5, 6, and 8, and then print them.
[spoiler]

Code: Select all

#include <stdio.h>

int main()
{
	int array1[50];
	array1[4] = 8;
	array1[5] = 20;
	array1[7] = 30;
	printf("%d, %d, %d\n", array1[4], array1[5], array1[7]);
	
	return 0;
}
[/spoiler]
Using only pointers: given a random string, print from the 5th character onwards.
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{
	char* s1 = "Random string.";
	char* s2 = s1 + 4;
	
	printf("%s\n", s2);

	return 0;
}
[/spoiler]
Given 2 strings of length 5, encrypt the first one using the second one using an XOR operation.
I'm having a hard time with this one. The only way I can think to do it would be: to get the ASCII values of both strings, then convert them to binary, and then do the XOR opperation. This seems beyond what we know though, so I'm guessing I just am thinking about it wrong.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Fri Jun 22, 2012 8:11 am

ChemDog wrote:Using only pointers: declare an array of signed integers and assing it a size of 50 integers.
Same as the last exercise but also assign positions 5, 6, and 8, and then print them.
You're using arrays and not pointers in your answer ;)
ChemDog wrote:Using only pointers: given a random string, print from the 5th character onwards.
All right :mrgreen:
ChemDog wrote:The only way I can think to do it would be: to get the ASCII values of both strings, then convert them to binary, and then do the XOR opperation. This seems beyond what we know though, so I'm guessing I just am thinking about it wrong.
You're thinking about it the right way, but you just believe it's complicated because you're confusing ASCII values, binary and characters. This 3 things are exactly the same one :mrgreen:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Sun Jun 24, 2012 8:08 am

m0skit0 wrote:In C, arrays can "decay" into pointers and viceversa, which means that arrays are equivalent to pointers. Thus for example char[] is the equivalent of char*.
This part has me a little confused :? . Here is my fix for the first two:
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int* a = (int*) malloc(50*sizeof(int));
	a[4] = 8;                                           // Are these pointers decayed into arrays or viceversa?
	a[5] = 20;
	a[7] = 30;
	printf("%d, %d, %d\n", b[4], a[5], a[7]);
	free(a);
	
	return 0;
}
[/spoiler]

Given 2 strings of length 5, encrypt the first one using the second one using an XOR operation.
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{	
	char* s1 = "buffy";
	char* s2 = "point";
	int a = (int)s1;
	int b = (int)s2;
	int c = a ^ b;
	s1 = (char*) c;
	printf("%08X\n", s1);
	return 0;
}
[/spoiler]
This was by far the hardest practice excercise yet :lol:

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by m0skit0 » Mon Jun 25, 2012 8:06 am

ChemDog wrote:This part has me a little confused :? . Here is my fix for the first two
You still using arrays. You aren't supposed to use array's syntax. Just work with a variable as a pointer.
ChemDog wrote:Given 2 strings of length 5, encrypt the first one using the second one using an XOR operation.
Why are you converting to char to int and then int to char? This is weird because (in 32-bit machines) char is 1 byte and int is 4 bytes. By converting to int you're using 4 times the memory you should use with no real reason. Just use char right away. char is a number. Don't ever forget that for a computer, everything is a number. Also watch out with casting because in C you can almost always cast anything to anything, and this can be a source of very hard to track bugs. Use casting only when you're very sure of what you're doing.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (VII)

Post by ChemDog » Tue Jun 26, 2012 2:37 am

Alright no more arrays:
[spoiler]

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int *a = (int*) malloc(50*sizeof(int));
	*(a+5) = 1;
	*(a+6) = 2;
	*(a+8) = 3;
	printf("%d, %d, %d\n", *(a+5), *(a+6), *(a+8));
	free(a);
	
	return 0;
}
[/spoiler]
m0skit0 wrote:Why are you converting to char to int and then int to char?
Well I started trying to do something like "int a = s1" but once I casted char to int, it didn't "click" that all of those conversions were unneccesary. So here is without all of the converting:
[spoiler]

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{	
	char* s1 = "buffy";
	char* s2 = "point";
	s1 = (int)s1 ^ (int)s2;
	printf("%08X\n", (unsigned int)s1);
	return 0;
}	

[/spoiler]
I think this is an ok time to use casting but if it is not I will try and figure something else out.

Post Reply

Return to “Programming and Security”