Thread: Program help

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Question Program help

    I am a newbie and trying to write a program that shifts letters in a circular fashion 5 times (ie: first line ABCDE, second line EABCD, third line DEABC, etc) using the following function definition:

    void shift(char *p1, char *p2, char *p3, char *p4, char *p5)

    First a question. What does the char *p1, etc represent?

    I know the function is calling a char but I don't understand what the '*' in from of the p1 signifies.

    Thanks for any help.

    Here's the basic code without the functon:
    Code:
    #include <stdio.h>
    
    void shift(char *p1, char *p2, char *p3, char *p4, char *p5);
    
    char main(void)
    {
      char c1='A',c2='B',c3='C',c4='D',c5='E';
      int cnt=0;
      printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); /*prints ABCDE*/
      if(cnt<=5){
    	shift(&c1,&c2,&c3,&c4,&c5);
    	printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); /*prints each  
                    line 5 times shifting the characters*/
                    cnt++;
    	}
      else
      return (0);
    }
    void shift(char *p1, char *p2, char *p3, char *p4,char *p5)
    {
    ?????????Here's where I am stuck
    }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    first of all you declared main as char, main returns int,
    so it should be int main() , then we will talk

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    Just so you know, I am an utter noob when it comes to programming (only recently started programming again after a 4 year break since college)so I know my advice is probably not great and my coding is not up to scratch but I think I can help a little. First off as mentioned, main returns an int, not a char. Second, your if statement will only execute once so you will not print your result 5 times as you want to. You need a loop there to do what you want. Also on a side point, you dont have to have an else in an if statement (just mentioned it because you have an empty else for your if)

    To answer your question, *p1 is a pointer to a char. Read up on pointers to figure out what is going on. I just quickly edited your code to do what you want it to so take a look below and see if you can figure out whats going on. I am sure this is probably not the best way to go about it but it works.... hey, like you I am just in the early stages of learning too

    Code:
    #include <stdio.h>
    
    void shift(char *p1, char *p2, char *p3, char *p4, char *p5);
    
    int main(void)
    {
        char c1='A',c2='B',c3='C',c4='D',c5='E';
        int cnt=0;
        printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); /*prints ABCDE*/
        while(cnt <= 4) /*prints each line 5 times shifting the characters*/
        {
             shift(&c1,&c2,&c3,&c4,&c5);
    	 printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); 
             cnt++;
        }
        return (0);
    }
    
    void shift(char *p1, char *p2, char *p3, char *p4,char *p5)
    {
        char temp;
        temp = *p5;
        *p5 = *p4;
        *p4 = *p3;
        *p3 = *p2;
        *p2 = *p1;
        *p1 = temp;   
    }
    Last edited by ./fsck; 08-24-2005 at 09:22 AM.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    the pointer let you pass a varaible to the function, in a way
    that let you modify the original value, without having to copy
    the amount and return it by the function.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Thanks for the help

    I don't know why I had char main() instead of int main(). I did a lot of cut and pasting in the compiler so it must have gotten missed while I was trying to figure out what to do in early versions of the code. I had declared some characters after #include <stdio.h> in the first versions I messed with. That's probaly where it came from.

    I'll review what had been posted in response and see if I can figure out what is going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM