Hiii im new
I was playing around with C and there are a few things i'd like to be cleared up
1.) So i can't get the length of an array of characters without keeping track of it myself?
2.) I was trying to write an encryption function - because you know.... funfunfun XDD - And i got a bit confused.
Code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
char Shift(char Achar,int Shifts)
{
bool Shiftleft =
Shifts <= 0;
if (Shiftleft)
Shifts=sqrt(pow(Shifts,2)); //i wonder if -1*val would be faster
int Lowerbound = 33;
int Remainder;
int Upperbound = 126;
Remainder =
Shifts%(Upperbound-Lowerbound);
if (Shiftleft)
{
if (Remainder>
(int)Achar-Lowerbound)
{
return (char)(Upperbound+(((int)Achar-Lowerbound)-Remainder)+1);
}
else return (char)((int)Achar-Remainder);
}
else
{
if (Remainder>
Upperbound-(int)Achar)
{
return (char)(Lowerbound+(Remainder-(Upperbound-(int)Achar))-1);
}
else return (char)((int)Achar+Remainder);
}
}
int Encrypt(char *Astring,char *Buffer,int *NewLength)
{
return 1;
}
int main(void)
{
char Test[0];
int Length;
Encrypt("Hello",Test,&Length);
printf("Length : %i\n",Length)
return 0;
}
I know it's not complete but the point was basically this. In delphi I could return a string very easily eg : copy("ab",1,1) would return 'a'
Now knowing the following i attempted to do the same in C.
On my phone i can't really see the terminal crash but it didn't work properly. So i read up on pointers because well they seemed to be the solution.
Encrypt takes three paramaters.
A string literal.
A pointer to an array of characters which will basically be the encrypted
String literal.
and finally a pointer of type int that i will use to store the length of the encrypted string.
Oh and 3) How effective is my shift function.
While fondling some pointers i just made a small program to memorise so I wouldn't forget how it works.
Code:
#include<stdio.h>
#include<stdlib.h>
int Changec(char *Arr)
{
Arr[1]='G';
return 1;
}
void Changei(int *Pointer)
{
*Pointer=3;
}
int main(void)
{
char Test[]="Hello";
int Temp;
Changec(Test);
printf("%c%c%c\n",Test[0],Test[1],Test[2]);
Changei(&Temp);
printf("%i",Temp);
return 0;
}
This is just really frustrating. Working with languages like delphi and c# just feels so much simpler but i must admit i love the way C feels ^.^ I'm doing this as preperation for university so any and all advise would be greayly appreciated.