Hello. I'm trying to learn C using K&R. I've had some background with Scheme and Java programming so I'm guessing that K&R is the path to take to learning C.
Earlier in the text, I wrote the following program, which allows the user to input a series of lines until EOF is invoked. Then, the program will print out the longest line entered by the user. Here's the code:
Code:
#include <stdio.h>
#define MAXLINE 1000
int getline(char to[], int max){
int count = 0;
int c;
while((c = getchar()) != '\n'){
to[count] = c;
count++;
}
return count;
}
void copy(char to[], char from[]){
int i = 0;
while((to[i] = from[i]) != '\0'){
i++;
}
}
main(){
char text[MAXLINE][MAXLINE];
char longestline[MAXLINE];
int i = 0;
int largest = 0;
int linelength = 0;
linelength = getline(text[i], MAXLINE);
while(text[i][0] != EOF){
if(linelength > largest){
largest = linelength;
copy(longestline, text[i]);
printf("%s%c", longestline, '\n'); //this part works (HEY)
}
linelength = getline(text[i], MAXLINE);
}
printf("%s", longestline);
}
Note that the line shouting HEY was just added to aid in debugging.
The way I understand how my program works, it seems that, called from main with the (main local?) variable longestline as an argument, the function copy copies the contents of [I]text into longestline such that, I can access its new value from main. (See the printf line shouting HEY right after the call to copy).
So far so good.
Then, I wrote a program which removes the "0x" part in hexadecimal numbers. Here is my code:
Code:
#include <stdio.h>
#include <string.h>
void removeOX(char hex[]){
int i = 0;
int limit = strlen(hex);
while(i < limit){
if(hex[i] == 'x'){
int j = 0;
int foo = i + 1;
char Xremoved[limit - i];
//printf("%s", "caught here");
while(j < (limit - i)){
Xremoved[j] = hex[foo];
j++;
foo++;
}
Xremoved[limit - i] = '\0';
printf("%s%s%s", "from removeOX: ", Xremoved, "\n");
hex = Xremoved;
printf("%s%s%s", "from removeOX hex: ", hex, "\n");
break;
}
else{
i++;
}
}
}
int main(){
char love[] = "0x8f";
removeOX(love);
printf("%s", love); //This bit doesn't work now!
}
Again, printf lines were added to aid in debugging.
The way I understand it, my use of functions are simillar here, i.e. they follow the following pattern:
1) Declare a variable in main.
2) Invoke a function, pass the variable declared in 1 to that function.
3) Function modifies its argument(s). Since I passed a declared variable, this declared variable also changes, as the function operates on its arguments.
4) modified argument == modified variable. I may now use the variable with its value modified.
I wonder what am I getting wrong here. Any help would be greatly appreciated. Much thanks.
(Note: I just started learning pointers and I tried to write the "0x" remover with pointers. No luck so far. However, my main concern for now is on my understanding of functions. How come the first one works and the "0x" remover won't?
In any case, should it seem appropriate, feel free to include pointers in your discussion .
Much thanks )