Thoughts on technology, science, reason, the free markets, politics, and other occasional topics. Please support this blog by visiting our google advertising partners! And remember, as the great William Hague, MP, says, "Only the Conservative Party will keep the pound!"

Wednesday, May 25, 2011

Why pass by reference supporting languages will always be superior...

Because in pass by reference (pointer) supporting languages like C, for one thing, you can switch the values of two variables, a and b without needing a temp variable, which you cannot in a language without pointers, or, at least far less straight forwardly as far as I can see. See code below. :-)

Signed,

An unrepentant acolyte of Bell Labs' Kernighan and Ritchie's C programming language.

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

int main(int argc, char *argv[])
{
char *a = (char*)malloc(sizeof(char)*8); //initializing string a
strcpy(a,"A string");

char *b = (char*)malloc(sizeof(char)*8); //initializing string b
strcpy(b,"B string");

printf("a is %s\n",a);
printf("b is %s\n",b);

printf("\nswitching values...\n\n");

////////////
//a = a + b;
////////////
if ( ( a = realloc(a, sizeof(char)*2*strlen(a) ) ) == NULL ) //Note: realloc only works for strings made with malloc in the first place
{
printf("Error reallocating memory to string a. System terminating with return value 1.\n");
return 1;
}

char *aPtr = &a[strlen(b)];
char *bPtr = &b[0];
while(*bPtr != '\0') *aPtr++ = *bPtr++;

////////////
//b = a - b;
////////////
aPtr = &a[0];
bPtr = &b[0];
while(*aPtr != a[strlen(b)]) *bPtr++ = *aPtr++;

////////////
//a = a - b;
////////////
int i = 0;
for( ; i < strlen(b); ++i) { a[i] = a[i + strlen(b)]; }
for( i = strlen(b); i < strlen(a); ++i) { a[i] = '\0'; }

printf("a is now %s\n",a);
printf("b is now %s\n",b);

free(a);
free(b);
free(aPtr);
free(bPtr);

printf("\nPress any key to continue...\n");
getc(stdin);
return 0;
}

Output of the above code looks like this on the console:

C:\Dev-Cpp\VariableTest>VariableTest.exe
a is A string
b is B string

switching values...

a is now B string
b is now A string

Press any key to continue...

1 comments:

Stuart said...

Very interesting. Thanks for posting that.

The Greatest Chatbot in the World

JS-Bot-Shell 1.0
Dialog

System Output

Your Last Input

Type Here and Hit "Enter"










Click below to purchase Occasional Memes, a compilation of original poetry by Frank Erdman

Support independent publishing: buy this book on Lulu.

About Me

My Photo
Sometime engineer, amateur pundit, amateur actor, amateur poet, cosmology and biology enthusiast, sometime critic, part Objectivist, part Realist, emphatic Empiricist, not above the occasional employment of mythical references for the sake of description in a sort of Ursula Goodenough-esque sort of way, politically centrist, fiscally slightly right, socially slightly left, believer in open global trade, a "Rent"-head, conneisseur of Armani, Louis Vuitton, sushi, fish tacos, lobster, Lovecraft, Barbara Streisand, Elton John, in short, one at home in the modern, ill-at home in the post-modern, and decidedly forlorn in the pre-modern

My Personal ChatBot

Click here to talk to my personal chatbot from http://www.a-i.com. A-I is an AI research centre in Tel-Aviv specializing in linguistic recognition.

Chat all you want, but remember, Chatbots have feelings too!

My Published Works


Testing Monte Carlo Algorithmic Systems,  A Sticky Minds Original Article  -  www.stickyminds.com  2009

Email Blogkinnetic

Blog Archive

William Hague, MP

William Hague, MP
Conservative Party Leader 1997 - 2001, Keeper of the Pound