Code:
#include "cstrf.hh"
int main() {
const char* s = "The quick brown fox";
const char* t = "jumped over the lazy dogs";
char* u = new char; // This works, but I don't know why
strcp(s, u);
std::cout<< "Length: " << strl(s) << '\n';
std::cout<< "Copy: (" << u << ")\n";
}
int strl(const char* s) {
int l = 0; //do local variables declared in functions have default initializers?
while(*s++ != 0) { l++; }
return l;
}
void strcp(const char* from, char* to) {
while(*to++ = *from++);
}
int strcmp(const char* s1, const char* s2) {
// -1 if s1 < s2, 0 if s1 == s2, +1 if s1 > s2
int c1;
int c2;
while(c1 += static_cast<int>(*s1++));
while(c2 += static_cast<int>(*s2++));
switch(c1 - c2) {
case 0:
return 0;
default:
if(c1 < c2) return -1;
else return 1;
}
}
header file :
Code:
#include <iostream>
int strl(const char*);
void strcp(const char*, char*);
int strcmp(const char*, const char*);
Does `new' instantiate an object and allocate memory for it? I was having trouble with things like
char* u = 0;
and
char u[] = "";
My guess is that the size of `u' is unknown in either of these cases and so the program will crash when it tries to do anything with `u' at all (it segfaulted on my end).
Thanks.