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

[Tutorial] Introduction to programming using C (VI)

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 (VI)

Post by m0skit0 » Tue Jun 05, 2012 7:03 am

JJS wrote:Interesting stuff: On x86 (Windows) where parameters are passed on the stack, you get the right-to-left output "1 20 1". On ARM (GNU Linux EABI) you get left-to-right "0 20 1". Parameters are passed in registers there. Things surely get even more confusing and hard to predict when you use more parameters so that some of them are passed in registers and others on the stack :?.
This is... C! :lol:
JJS wrote:Bottom line: Code like this is not portable.
This is only one of its bad characteristics.
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 (VI)

Post by Wdingdong » Tue Jun 05, 2012 12:20 pm

Oh, now I got it. I thought that, while evaluating parameters from right to left, the far right parameter is given to far left '%d' symbol. Also, the output were symmetrical. But, now I got that it is far right '%d' symbol only for the far right parameter. :oops:
Advertising
// Big thanks to people who share information !!!

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

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

Post by ChemDog » Mon Jul 02, 2012 3:03 am

m0skit0 wrote: Also you can do it in a different way. If you want, I'll challenge you to find another solution ;)
[spoiler]

Code: Select all

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

int main()
{
   char* s1 = ("I am me, you are you.");
   char s2[20];               
   int s1len = strlen(s1);                //string 1 length
   int ssmelen = strlen(strstr(s1, "me"));  //sub string "me" length
   strncpy(s2, s1, s1len - ssmelen + 2);    //copy characters equal to the difference of s1len and ssmelen and add two to account for "me"
   *(s2+(s1len - ssmelen +2)) = 0;        // sets first value after string to null 
   printf("%s\n",s2);
   return 0;
}
[/spoiler]
I think this could be an alternative to using memset() :)

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

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

Post by snailface » Mon Jul 02, 2012 4:06 am

Make a program that having the string "Hello dang world", manipulates it and prints "Hello world".
[spoiler]

Code: Select all

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

int main ()
{
    char str[]="Hello dang world.";

    memmove(str+6,str+11,strlen(str+11)+1);

    printf("%s",str);
    return 0;
}
[/spoiler]
Make a program that erases everything on a string after the string "me" (this one included).
[spoiler]

Code: Select all

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

int main (){
char str[]="This is me, a monster!";
char find[]="me";

*(strstr(str,find) + strlen(find))=0;
printf("%s",str);
return 0;
}
[/spoiler]
Image

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

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

Post by m0skit0 » Mon Jul 02, 2012 8:16 am

@ChemDog:

Code: Select all

*(s2+(s1len - ssmelen +2)) = 0;
Nice idea ;)

@snailface: Ok, but a comment ;)

Code: Select all

memmove(str+6,str+11,strlen(str+11)+1);
Cool function using, but your code is poorly understandable, maintainable and scalable. Using less lines does not mean better programming (in fact usually it means poor readability). You have to make your code easy to understand for others as well. Nesting functions into one another makes the code more difficult to read and understand, and most of time also limits its flexibility and also debugging. Also using literals is strongly discouraged.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

SifJar
Posts: 251
Joined: Tue Jan 11, 2011 10:19 pm

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

Post by SifJar » Thu Mar 07, 2013 10:58 pm

I know this is a tad old, but I was trying the exercises (the first was very easy, not bothering to post here), and wanted to know if my solution for the second exercise contains any "bad practices" etc.

Make a program that concatenates 2 strings and shows the resulting string and its length.

[spoiler]

Code: Select all

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

int main(void) {

	char* string1 = "Hello ";
	char* string2 = "World";

	int length = strlen(string1) + strlen(string2);
	char* string3 = malloc(length);
	strcpy(string3, string1);
	strcat(string3, string2);

	printf("%s\nLength = %d", string3, length);

	return 0;
}
[/spoiler]

I intend to do the other exercises as well, and follow through any exercises in later parts of the tutorial as well, but I just wanted to check if I am making any big mistakes before moving on (of course, I have tested the program and it works fine, but that doesn't necessarily mean it is well coded).

EDIT: Some more solutions:

Make a program that shows the average length of 4 strings.
[spoiler]

Code: Select all

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

int main(void)
{
	char* string0 = "This is the first string";
	char* string1 = "This is the second string";
	char* string2 = "This is the third string";
	char* string3 = "This is the fourth string";

	double average = (double) strlen(string0) + (double) strlen(string1) + (double) strlen(string2) + (double) strlen(string3);
	average /= 4.0;

	printf("Average length = %g", average);
	return 0;
}
[/spoiler]

Make a program that having the string "Hello dang world", manipulates it and prints "Hello world".
[spoiler]

Code: Select all

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

int main(void)
{
	char* string0 = "Hello dang world";
	char string1[20];
	memset(string1, 0, 20);
	strncpy(string1, string0, 6);
	strcat(string1, "world");
	printf("Final string = %s", string1);
	return 0;
}
[/spoiler]

Make a program that erases everything on a string after the string "me" (this one included).
[spoiler]

Code: Select all

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

int main(void)
{
	char* string0 = "This string has me in it";
	int length = strlen(string0) - strlen(strstr(string0, "me")) + 2; //+2 to include "me"
	char* string2 = malloc(length);
	strncpy(string2, string0, length);
	printf("Original String = %s\nNew String = %s", string0, string2);
	return 0;
}
[/spoiler]

This doesn't give quite the expected output. There are random characters appended to the output of "string2"; obviously some error in allocating memory or something, but I can't figure out what's wrong. I have allocated string2 "length" bytes of memory, then copied "length" bytes into it; why is it not working right?

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

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

Post by JJS » Sat Mar 09, 2013 1:38 pm

You must watch out that you allocate the right amount of memory for a string. You always need one more byte than the string length to hold the terminating '\0' character.

Code: Select all

s = malloc(strlen("an example") + 1);
Also strncpy() will not append the terminating 0 if the string it has to write is as long as the maximum length. This means that e.g. the following code

Code: Select all

s = malloc(5);
strncpy(s, "abcde", 5);
will produce an unterminated string. Printing that string would therefore give you additional garbage bytes.

SifJar
Posts: 251
Joined: Tue Jan 11, 2011 10:19 pm

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

Post by SifJar » Sat Mar 09, 2013 3:54 pm

JJS wrote:You must watch out that you allocate the right amount of memory for a string. You always need one more byte than the string length to hold the terminating '\0' character.

Code: Select all

s = malloc(strlen("an example") + 1);
Also strncpy() will not append the terminating 0 if the string it has to write is as long as the maximum length. This means that e.g. the following code

Code: Select all

s = malloc(5);
strncpy(s, "abcde", 5);
will produce an unterminated string. Printing that string would therefore give you additional garbage bytes.
Ah, that explains things. Thanks. Got it fixed then:

[spoiler]

Code: Select all

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

int main(void)
{
	char* string0 = "This string has me in it";
	int length = strlen(string0) - strlen(strstr(string0, "me")) + 2; //+2 to include "me"
	char* string2 = malloc(length+1);
	memset(string2, 0, length+1);
	strncpy(string2, string0, length);
	printf("Original String = %s\nNew String = %s", string0, string2);
	return 0;
}
[/spoiler]

User avatar
pspfanMOHH
Posts: 660
Joined: Sat Jun 11, 2011 9:16 pm
Location: Grand Line, New World

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

Post by pspfanMOHH » Sat Apr 27, 2013 1:36 am

Might want to explain using string[#] its 1 less(I know you showed hello is 5 but it has a 0 but adding that for "string[#]" can help to say how its similar), and that you can use this to ask the user for letters(char) and not just numbers.
Thanks m0skit0 this warmed my memory up.

User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

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

Post by Acid_Snake » Sat Apr 27, 2013 1:55 pm

JJS wrote:You must watch out that you allocate the right amount of memory for a string. You always need one more byte than the string length to hold the terminating '\0' character.

Code: Select all

s = malloc(strlen("an example") + 1);
Also strncpy() will not append the terminating 0 if the string it has to write is as long as the maximum length. This means that e.g. the following code

Code: Select all

s = malloc(5);
strncpy(s, "abcde", 5);
will produce an unterminated string. Printing that string would therefore give you additional garbage bytes.
and that's why I always allocate 128 chars for each string, it's basically enough for most stuff

Post Reply

Return to “Programming and Security”