pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; Hello, I have some code here, would you mind checking to see if I changed all the parts I could ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    pointers

    Hello,

    I have some code here, would you mind checking to see if I changed all the parts I could to pointers?

    Thanks,
    Sarah

    Code:
     #include <stdio.h>
    #include <string.h>
    
    
    /*****************************************************************************
    * name: main
    * returns: int; 0 for success
    *
    * Purpose:takes an input of an array and rotates its elements.  
    * Requires:  string.h stdio.h getchar fn.
    * Dependencies:  none
    *****************************************************************************/
    
    
    int main (void)
    {
    
    	//variables in the function
    		char arr[21] = {0};	// this is the character array
    		int i = 0;   		// this is a counter
    		char a;			// this is a variable for the getchar fn.
    		char temp;		// this is a temporary variable
    		int n;			// this is the length of the string
    		int t;			// t is the number of times the rotation occurs
    
    //get the input from the user
    printf("Enter up to 20 characters, then press ENTER.\n");
    
    
    	//this will get the character entered from the user as long as the user does not enter a newline (ENTER)
    while ((a = getchar()) != '\n' )
    
    {
        *(arr +i++) = a;		//increments the element in the array, assigns it to a.
    }
      *(arr +i) = NULL;
    
    //this prints the string back to the user
    printf( "\nThe string is: %s\n\n", arr);
    
    
    //this assigns n the value found from the strlen function
    n = strlen (arr);
    
    
    //this rotates the string n times.
    for (t = 1; t < n; t++)
    {
    
    
    //this loop assigns the variable at element 0 to temp, them prints the new string with temp at the end.
    temp = *(arr+ 0);
    for (i = 1; i < 20; i++)
        arr [i-1] = *(arr +i);
    
    arr[n-1] = temp;
    
    printf("\nThe number %d string is: %s\n", t, arr);
    
    }
    
    return 0;}
    
    //end main

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably "change everything to pointers" means "no more array notation". So hit Ctrl-F, type a [ where it says "find what:" and see if it finds any brackets. If it does, you didn't change them all.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    arr [i-1]

    to change arr [i-1] to a pointer: *(arr +i -1)
    ???

    Or do I have it wrong/
    Thanks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    arr[i-1] is the same as *(arr+i-1), yes.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    15

    thanks!

    thanks a billion.

  6. #6
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Still *(arr +i) does exactly the same thing as arr[i], except it 1. shows you understand that they are the same & 2. Saves afew microseconds on the compiler's part. According to the first post, you missed one:
    Code:
    char arr[21] = {0};	// this is the character array
    Should be char *arr.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by P4R4N01D View Post
    Still *(arr +i) does exactly the same thing as arr[i], except it 1. shows you understand that they are the same & 2. Saves afew microseconds on the compiler's part. According to the first post, you missed one:
    Code:
    char arr[21] = {0};	// this is the character array
    Should be char *arr.
    And if you use char *arr, then you need to assign the pointer some memory, or bad things will happen. Exactly what kind of bad things will depend on the value that an unassigned pointer happens to have in this particular instance - but most likely some form of crash.

    I would also expect that the "savings" on the compiler will be absolutely nothing - if anything arr[i] is shorter, so it will help the compiler parse it. If you really want to save compiler time, use shortest possible variables!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

Tags for this Thread


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