Am I using pointers correctly here?

This is a discussion on Am I using pointers correctly here? within the C Programming forums, part of the General Programming Boards category; I have a simple program I am coding for my class and so far I have got the program to ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    Am I using pointers correctly here?

    I have a simple program I am coding for my class and so far I have got the program to do what I want it to do, however I am not sure if I am using pointers as my professor had asked us to use in this program correctly.

    Could someone take a look at this code and let me know if I am actually using pointers here to convert a line of text to uppercase and then lowercase?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    	/* Variable declarations */
    	char text[26];
    	int i;
    	char *text_Ptr = text;
    	
    	/* Prompt user for line of text */
    	printf ("\nEnter a line of text (up to 25 characters):\n");
    	gets(text);
    	
    	/* Convert and output the text in uppercase characters */
    	printf ("\nThe line of text in uppercase is:\n");
    	
    	i = 0;
    	while ( text[i] != '\0')
    	{
    		putchar( toupper(text_Ptr[i++]) ); <--- should the text_Ptr have a * here?
    	}
    	
    	/* Convert and output the text in lowercase characters */
    	printf ("\n\nThe line of text in lowercase is:\n");
    	
    	i = 0;
    	while ( text[i] != '\0')
    	{
    		putchar( tolower(text_Ptr[i++]) ); <--- should the text_Ptr have a * here?
    	}
    	
    	printf ("\n");
    	
    } /* end main */
    I seem to believe that I am suppose to have an * next to text_Ptr throughout the code, however when I put the * the code does not compile correctly and gives this error "invalid type argument of `unary *'".

    Any help is much appreciated.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kisiellll View Post
    I seem to believe that I am suppose to have an * next to text_Ptr throughout the code, however when I put the * the code does not compile correctly and gives this error "invalid type argument of `unary *'".
    No, the compiler is right and you are wrong (in your conviction, not your code).

    Ie. there SHOULD NOT be an asterisk there, that would be dereferencing the pointer. The asterisk is "over-loaded" in C (it can have more than one meaning, depending on context).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    The index operator [] also dereferences the pointer, so you are fine.
    However, the code is not fine. You should take a look at these:
    - Gets - why it's bad and why you should avoid it.
    - Implicit main - why it's frowned over and why you should avoid it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Yeah I know about using gets and my style of using main, but its what my professor wants us to use for some reason. Its an introductory course so I think hes just trying to keep it simple for now, although I feel it could create bad habits.

    Anyways I revised my code and I think I'm doing the entire process correctly using pointers as my professor had assigned.

    Heres the new code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    	/* Variable declarations */
    	char text[26];
    	char *textPtr;
    	
    	/* Prompt user for line of text */
    	printf ("\nEnter a line of text (up to 25 characters):\n");
    	gets(text);
    	
    	textPtr = text; /* point textPtr to text */
    	
    	/* Convert and output the text in uppercase characters */
    	printf ("\nThe line of text in uppercase is:\n");
    	
    	/* Loop to convert to uppercase */
    	while ( *textPtr )
    	{
    		*textPtr = toupper(*textPtr);
    		textPtr++;
    	} /* end while */
    	
    	/* Output uppercase line of text */
    	printf("%s\n", text);
    	
    	textPtr = text; /* Convert textPtr back to original text */
    	
    	/* Convert and output the text in lowercase characters */
    	printf ("\nThe line of text in lowercase is:\n");
    	
    	/* Loop to convert to lowercase */
    	while ( *textPtr )
    	{
    		*textPtr = tolower(*textPtr);
    		textPtr++;
    	} /* end while */
    	
    	/* Output lowercase line of text */
    	printf("%s\n", text);
    	
    } /* end main */

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Another bad teacher... does your teacher care if you use proper code, though?
    If your professor does, remember to slam the course after you're done with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Do I understand pointers correctly?
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 01-05-2006, 08:30 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 08:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21