![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 7
| 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);
}
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!
}
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 ) |
| skytreader is offline | |
| | #2 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,368
| Note that an array name is not a variable so it cannot be assigned to ie it's not an lvalue. So all the processing done in removeOX() is lost after it returns because love still points to the original string. Your best bet is to return a pointer instead of void from removeOX() to the hex string minus the leading "0x". |
| itCbitC is offline | |
| | #3 |
| Registered User Join Date: Sep 2007
Posts: 420
| Your observations on the K&R code are correct. However, it's a bit more subtle than it appears, which is why your code is not working as you expect. The first thing to know is that C is a pass by value language. When you pass an object into a function, only a copy is passed. When you modify that object in the function, it is not changed in the caller: Code: #include <stdio.h>
static void f(int i)
{
i = 10;
}
int main(void)
{
int i = 5;
f(i);
printf("%d\n", i); /* i is still 5 */
return 0;
}
Code: static void f(char foo[]) /* foo is actually a pointer here, despite the [] syntax */
{
foo = new_value;
}
Code: static void f(char foo[])
{
foo[0] = 'x';
}
But before you do that, you'll have to look at your code that copies into Xremoved. Xremoved is an array of (limit - i) size. That means the last element of the array must be (limit - i - 1). To make this clear, say you have an array of size 3: this only has elements 0, 1, and 2. Your code is writing one byte beyond the end of the array. Despite the fact that pointers and arrays get jumbled up like this, it's important to remember that they are distinct. You can, most of the time, treat an array like a pointer but it is not a pointer, and there are a few times where this distinction matters. |
| cas is offline | |
| | #4 |
| Registered User Join Date: Nov 2009
Posts: 7
| cas you got it all...much thanks. I'll have more light now in tackling this problem. Will try it out again as soon as I get some sleep...Again, thanks (--,) |
| skytreader is offline | |
![]() |
| Tags |
| functions, pointers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| COM port CreateFile -> Close Handle Works great, unless the USB device is unplugged, | morty346 | Windows Programming | 1 | 08-04-2009 08:57 AM |
| Can anybody show me why this works?? | tzuch | C Programming | 2 | 03-29-2008 09:03 AM |
| fprintf works in one function but not in the other. | smegly | C Programming | 11 | 05-25-2004 03:30 PM |
| Programs works one way, not another. VC++ 6.0 help. | ethic | Windows Programming | 4 | 12-10-2002 10:29 PM |
| it never works... | Ryce | Game Programming | 5 | 08-30-2001 07:31 PM |