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.
Wdingdong
Posts: 92
Joined: Tue Aug 02, 2011 5:34 pm

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

Post by Wdingdong » Mon Jun 11, 2012 6:23 pm

@waratte: Thanks for the explanation man.
m0skit0 wrote:Well I see you already know functions and flow control, that's nice ;) Although I will recover this exercises soon enough :)
I thought you want us to learn it by ourselves. So,I've gained some basic knowledge about it. But, whatever I know, I lay no claim on it, it's all because of you and this forum :)

Here are my exercise answers:
[spoiler]

Code: Select all

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

void exercise1And2()	//first and second exercise combined 
{
	int* a = malloc(50*sizeof(int));
	*(a+4) = 1;
	*(a+5) = 2;
	*(a+7) = 3;
	printf("%d %d %d\n",*(a+4),*(a+5),*(a+7));
}

void exercise3()	//Using only pointers: given a random string, print from the 5th character onwards.
{
	char* s2 = "Not galaxy s2";
	printf("%s\n",(s2+4));
}//Is it this much only?

void exercise4()	//XOR operation
{
	char s[5] = "Uncle";
	char s1[5] = "Jessy";
	int a = s[0] ^ s1[0];
	int b = s[1] ^ s1[1];
	int c = s[2] ^ s1[2];
	int d = s[3] ^ s1[3];
	int e = s[4] ^ s1[4];
	printf("%d %d %d %d %d",a,b,c,d,e);
}

int main()
{
	exercise1And2();
	exercise3();
	exercise4();
	return 0;
}

[/spoiler]
Corrections accepted :mrgreen:
Advertising
// Big thanks to people who share information !!!

snailface
Posts: 95
Joined: Tue May 24, 2011 8:02 pm

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

Post by snailface » Mon Jun 11, 2012 11:10 pm

Here is my submission, Prof. Moskito. :D I hope I pass.

[spoiler]

Code: Select all

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

    int main()
    {
//Using only pointers: declare an array of signed integers and assing it a size of 50 integers.
printf("Ex1\n\n");

    int *array=(int *)malloc(50);

//Same as the last exercise but also assign positions 5, 6, and 8, and then print them.\n\n");
printf("Ex2\n\n");

    int *array2=(int *)malloc(50);
    *(array2+5)=76;
    *(array2+6)=79;
    *(array2+8)=76;
    printf("%c %c %c\n\n",*(array2+5),*(array2+6),*(array2+8));

//Using only pointers: given a random string, print from the 5th character onwards.\n\n");
printf("Ex3\n\n");

    char *wololo="Skip A string.\n\n";
    printf(wololo+5);

//Given 2 strings of length 5, encrypt the first one using the second one using an XOR operation.\n\n");
printf("Ex4\n\n");

    char *str=(char*)malloc(6);
    char *str2=(char*)malloc(6);
    memcpy(str,"Snail",6);
    memcpy(str2,"faces",6);
    int x=2;

    while(x--)
    {

       do
       {
           *str^= *str2;
           str++;
           str2++;

       }while(*str);

           str-=5;
           str2-=5;
           printf(str);
           printf("\n");
    }

getch();
return 0;
}

[/spoiler]
Advertising
Image

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

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

Post by Wdingdong » Tue Jun 12, 2012 5:24 am

What's the difference between these two:

Code: Select all

int* a = (int*) malloc(sizeof(int));
and

Code: Select all

int* a = malloc(sizeof(int));
// Big thanks to people who share information !!!

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 » Tue Jun 12, 2012 7:18 am

Wdingdong wrote:Corrections accepted :mrgreen:
Sure I will :mrgreen:
Wdingdong wrote:void exercise1And2()
I said positions 5, 6, and 8, not 5th, 6th and 8th :) You have a memory leak here (you didn't free the memory).
Wdingdong wrote:void exercise4()
Sorry, my bad...The result of the encryption must be another string. You can print the resulting string in hexadecimal representation since maybe some (most) of resulting the characters are not printable.
Wdingdong wrote:What's the difference between these two
If you don't use the first one, any decent compiler will issue a warning, since malloc() returns void* and not int*.
snailface wrote:printf("Ex1\n\n");
Memory leak!
snailface wrote:printf("Ex2\n\n");
Memory leak!
snailface wrote:printf(wololo+5);
Huh? Does not compile. This is not Python! :lol:
snailface wrote: char *str=(char*)malloc(6);
char *str2=(char*)malloc(6);
memcpy(str,"Snail",6);
memcpy(str2,"faces",6);
You don't need to malloc() (you also have 2 memory leaks here as well). To copy strings, use strcpy() (or strncpy()).
snailface wrote: while(x--)
{

do
{
*str^= *str2;
str++;
str2++;

}while(*str);

str-=5;
str2-=5;
printf(str);
printf("\n");
}
:?: This is way too much complicated for such a simple thing... You don't even need to use loops. Also, why you initialize x to 2? Makes no sense to me.
snailface wrote:getch();
Please avoid this, dear Windows user :mrgreen: If you don't want your console to close, use another IDE.
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 » Tue Jun 12, 2012 9:03 am

m0skit0 wrote:I said positions 5, 6, and 8, not 5th, 6th and 8th You have a memory leak here (you didn't free the memory).
My English problem I think :oops: and added free();
m0skit0 wrote:Sorry, my bad...The result of the encryption must be another string. You can print the resulting string in hexadecimal representation since maybe some (most) of resulting the characters are not printable.
Fixed(It's still wrong I think).
snailface wrote:while(x--)
{

do
{
*str^= *str2;
str++;
str2++;

}while(*str);

str-=5;
str2-=5;
printf(str);
printf("\n");
}
I had a nightmare due to this :mrgreen:

Revision0.1 8-) :
[spoiler]

Code: Select all

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

void exercise1And2()	//first and second exercise combined
{
	int* a = 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);
}

void exercise3()	//Using only pointers: given a random string, print from the 5th character onwards.
{
	char* s2 = "Not galaxy s2";
	printf("%s\n",(s2+4));
}//Is it this much only?

void exercise4()	//XOR operation
{
	char* s = "Uncle";
	char* s1 = "Jessy";
	char x[5];
	x[0] = s[0] ^ s1[0];
	x[1] = s[1] ^ s1[1];
	x[2] = s[2] ^ s1[2];
	x[3] = s[3] ^ s1[3];
	x[4] = s[4] ^ s1[4];
	printf("%x%x%x%x%x",x[0],x[1],x[2],x[3],x[4]);
}

int main()
{
	exercise1And2();
	exercise3();
	exercise4();
	return 0;
}
[/spoiler]
// Big thanks to people who share information !!!

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 » Tue Jun 12, 2012 9:52 am

Wdingdong wrote:exercise1And2()
Yeah! :)
Wdingdong wrote:exercise4()
Correct!
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
Salahkun
Posts: 62
Joined: Wed Feb 29, 2012 4:52 pm

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

Post by Salahkun » Tue Jun 12, 2012 9:58 am

Mr moskito would you like to make more tuts of psp programming when you have some free time
ImageImage
The Mana World Player Since 2005

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 » Tue Jun 12, 2012 10:10 am

Nope, I won't. Anyway, this is valid for any platform, it is standard C.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

snailface
Posts: 95
Joined: Tue May 24, 2011 8:02 pm

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

Post by snailface » Tue Jun 12, 2012 9:44 pm

Code: Select all

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");
char *str2="Faces";                 //I can here because it the string doesn't need modification

   int x=0;

       for(x=0;x<10+1;x++){
           if( !(x%5) )printf("%s\n",str);   //prints unencrypted 'Snail' at x=0, encrypted x=5, decrypted x=10
           *(str + x%5) ^= *(str2 + x%5);    //xors 'Snail' character for character with 'faces'
       }
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. :)

Code: Select all

int *array=(int *)malloc(50);
free(array);   //thx FrEdDy :)
Memory leak!
Can you please explain why? I've seen that (int*) typecast convention with malloc() used in several books including K&R.

Code: Select all

char *wololo="Skip A string.\n\n";
    printf(wololo+5);
Huh? Does not compile. This is not Python! :lol:
I know this is unorthodox code, but it is perfectly valid code and I've tested it in GCC and VS and it compiles and works with no warnings. The first arg in printf is looking for a const char* format --which is what wololo+5 points to in this example.

Still, I should probably avoid using this weird method since it confuses and frightens people. :lol:
Last edited by snailface on Tue Jun 12, 2012 10:18 pm, edited 3 times in total.
Image

User avatar
FrEdDy
HBL Collaborator
Posts: 243
Joined: Mon Sep 27, 2010 7:08 pm
Contact:

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

Post by FrEdDy » Tue Jun 12, 2012 9:50 pm

Repeat after me: always free your memory,always free your memory...
https://github.com/freddy-156
<@n00b81> FREDDY CUTTIES

Post Reply

Return to “Programming and Security”