<$BlogRSDUrl$>

Wednesday, May 19, 2004

Hoping someone can help me with what is probably a silly question. I am to take user input, put it into uppercase, then lowercase. The code below does output the uppercase, except I am missing the first character. I don't get any output for the lowercase.

Thanks in advance..any help is appreciated!



#include
#include


void main (void)

{

char text[70];

char *text_ptr=text;

int i;


printf ("\nEnter a line of text (up to 69 [COLOR=blue]characters):\n");

gets(text);


text[70]='\0';>



printf("\nThe line of text in uppercase [COLOR=blue]is:\n\n");

i=0;

while(*text_ptr)

{
*text_ptr++;

putchar(toupper(*text_ptr));

}

printf("\n\nThe line of text in lowercase is:\n\n");

i=0;

while(*text_ptr)

{

*text_ptr++;

putchar(tolower(*text_ptr));

}

}

--------------------------------------------------------------------------------

Become A Member, Free!
Character Array and Pointer
Scorpions4ever
For printing in uppercase, flip the order of the two statements in the loop, (i.e.) uppercasing the pointer and then increment it. You're incrementing the pointer first and then uppercasing, which is why you're missing the first char:

while(*text_ptr)
{
putchar(toupper(*text_ptr));
text_ptr++;
}

As for the lowercasing part, you forgot to reset your pointer to the beginning of the string again. As before, also change the order of your statements inside the loop and you should be good to go.

printf("\n\nThe line of text in lowercase is:\n\n");
text_ptr = text; /* <--- Point text_ptr to the beginning of the string again */
while(*text_ptr)
{
putchar(tolower(*text_ptr));
text_ptr++;
}


I realize that you're probably a beginner to C programming, so I'll give you a little friendly advice. Using gets() is highly discouraged for any serious programming. Consider using fgets() or fread() instead. See the BUGS section in http://www.openbsd.org/cgi-bin/man.cgi?query=gets&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html for why

Saturday, April 24, 2004

Pointers in C
Pointers are not exclusive to functions, but this seems a good place to introduce the pointer type.

Imagine that we have an int called i. Its address could be represented by the symbol &i. If the pointer is to be stored as a variable, it should be stored like this.


int *pi = &i;
int * is the notation for a pointer to an int. & is the operator which returns the address of its argument. When it is used, as in &i we say it is referencing i.
The opposite operator, which gives the value at the end of the pointer is *. An example of use, known as de-referencing pi, would be


i = *pi;
Take care not to confuse the many uses of the * sign; Multiplication, pointer declaration and pointer de-referencing.

This is a very confusing subject, so let us illustrate it with an example. The following function fiddle takes two arguments, x is an int while y is a pointer to int. It changes both values.



fiddle(int x, int *y)
{ printf(" Starting fiddle: x = %d, y = %d\n", x, *y);
x ++;
(*y)++;
printf("Finishing fiddle: x = %d, y = %d\n", x, *y);
}
since y is a pointer, we must de-reference it before incrementing its value.

A very simple program to call this function might be as follows.



main()
{ int i = 0;
int j = 0;

printf(" Starting main : i = %d, j = %d\n", i, j);
printf("Calling fiddle now\n");.
fiddle(i, &j);
printf("Returned from fiddle\n");
printf("Finishing main : i = %d, j = %d\n", i, j);
}
Note here how a pointer to int is created using the & operator within the call fiddle(i, &j);.
The result of running the program will look like this.



Starting main : i = 0 ,j = 0
Calling fiddle now
Starting fiddle: x = 0, y = 0
Finishing fiddle: x = 1, y = 1
Returned from fiddle
Finishing main : i = 0, j = 1
After the return from fiddle the value of i is unchanged while j, which was passed as a pointer, has changed.

To summarise, if you wish to use arguments to modify the value of variables from a function, these arguments must be passed as pointers, and de-referenced within the function.

Where the value of an argument isn't modified, the value can be passed without any worries about pointers.


array access pointer equiva
arr[0] *arr
arr[2] *(arr+2)
arr[n] *(arr+n)
There are some differences between arrays and pointers. The array is treated as a constant in the function where it is declared. This means that we can modify the values in the array, but not the array itself, so statements like arr ++ are illegal, but arr[n] ++ is legal.

Since an array is like a pointer, we can pass an array to a function, and modify elements of that array without having to worry about referencing and de-referencing. Since the array is implemented as a hidden pointer, all the difficult stuff gets done automatically.

A function which expects to be passed an array can declare that parameter in one of two ways.





Either of these definitions is independent of the size of the array being passed. This is met most frequently in the case of character strings, which are implemented as an array of type char. This could be declared as char string[]; but is most frequently written as char *string; In the same way, the argument vector argv is an array of strings which can be supplied to function main. It can be declared as one of the following.





Don't panic if you find pointers confusing. While you will inevitably meet pointers in the form of strings, or as variable arguments for functions, they need not be used in most other simple types of programs.


This page is powered by Blogger. Isn't yours?