Thread: Home work question

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Home work question

    insert
    Code:
    #include <stdio.h>
    char a,b,c,d,e,p1,p2,p3,p4,p5;
    
    
    void shift(char *p1,  char *p2, char *p3, char *p4, char *p5){
    
    	char *p6;
    
    	*p1 = *p6;
    	*p2 = *p1;
    	*p3 = *p2;
    	*p4 = *p3;
    	*p5 = *p4;
    	*p6 = *p5;
    
    
    } 
    
    int main(void){
    
    	char a,b,c,d,e,p1,p2,p3,p4,p5;
    
    	printf("Please enter 5 different characters: ");
    	scanf("%c,%c,%c,%c,%c",&a,&b,&c,&d,&e);
    	printf("\nYou entered: %c%c%c%c%c\nShifting...",a,b,c,d,e);
    
    	shift(a,b,c,d,e);
    	printf("%c%c%c%c%c",p1,p2,p3,p4,p5);
    
    
    }

    Here is my code, its a very simple program to shift 5 stored character values around clockwise, however I keep getting this error message:

    insert
    Code:
    8-6.c: In function `main':
    8-6.c:29: warning: passing arg 1 of `shift' makes pointer from integer without a cast
    8-6.c:29: warning: passing arg 2 of `shift' makes pointer from integer without a cast
    8-6.c:29: warning: passing arg 3 of `shift' makes pointer from integer without a cast
    8-6.c:29: warning: passing arg 4 of `shift' makes pointer from integer without a cast
    8-6.c:29: warning: passing arg 5 of `shift' makes pointer from integer without a cast

    If I could get some help that would be fantastic as well as much appreciated!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    you're calling your function with char variables, but then you use char pointers in the function, that's what the message is saying.

    Either way, what do you mean by shifting the value?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Basically it just shifts the order in which each are printed ie.:

    if ABCDE is entered the function returns with BCDEA etc.

    Sorry, I'm pretty new at this what exactally do I need to change to fix the problem? I dont know if that question is allowed under the homework policy FAQ, but I believe it is.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    I fixed the error, however something in the program isn't working my out put is now:
    Code:
    Please enter 5 different characters: ABCDE
    
    You entered: ότ
    Shifting...

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    74
    you can change the order that you print to the screen.
    For example, printf("...", b, c, d, e, a);

    Kinda of a trick, since you're not really changing anything.

    If you want to use that function you have, then you have to change somethings.
    First just use char types, no pointer.
    Also, assign to p6 some value first, like for example p1, then you assign to p1 the value of p2
    , to p2 the value of p3, and so on.

    btw, you have a,b,c,... declared as global and at main, you don't need that.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Thanks alot!!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    shift(a,b,c,d,e);
    
    // should be
    
    shift(&a, &b , &c, &d, &e)
    You are correct that you do need pointers as inputs to your function because you are changing the actual values of external variables... but you call the function by value instead of passing in pointers. The & operator says "address of" which is a pointer.

    You also need to asign a preliminary value to p6, if you are using the chars as text p6 = ' ' (a space) should work.

    Also your final printf should be printing a b c d and e, not p1 p2 p3 p4 and p5.
    Last edited by CommonTater; 11-01-2010 at 11:36 PM.

  8. #8
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    see every line carefully

    Code:
    #include <stdio.h>
    char a,b,c,d,e,p1,p2,p3,p4,p5;
    
    void shift(char *p1,  char *p2, char *p3, char *p4, char *p5){
    
    	char *p6;
    
    	*p6 = *p1;	
    	*p1 = *p2;
    	*p2 = *p3;
    	*p3 = *p4;
    	*p4 = *p5;
    	*p5 = *p6;
    } 
    
    int main(void){
    
    	char a,b,c,d,e,f;
    
    	printf("Please enter 5 different characters: \n\n");
    	fflush(stdin);
    	scanf("%c %c %c %c %c",&a,&b,&c,&d,&e);
    	printf("\nYou entered: %c %c %c %c %c \n\nShifting...  ",a,b,c,d,e);
    
    	shift(&a,&b,&c,&d,&e);
    	printf("%c %c %c %c %c",a,b,c,d,e);
    
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, next step... you don't need to define p1, p2 , p3, p4 and p5 at the top of your file. Those are defined in you Shift() function header and those are the ones that are used inside the function.

    The same is true for a,b,c,d and e at the top of the file. You redefine them inside main.

    f defined in main is also unused.

    The global copies at the top of your file will remain unused. You can simply delete that line.
    Last edited by CommonTater; 11-02-2010 at 03:17 AM.

  10. #10
    C-no_Ob Bennie98's Avatar
    Join Date
    Oct 2010
    Location
    Ledeberg, Ghent, East-Flanders, Belgium
    Posts
    49
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void){
        int content, inhoud2, length, lengte;
        char naam[250]; 
        char voor[250];
        
        printf("Geef uw voornaam in: ");
        scanf("%s",voor);
        printf("Geef uw naam in: ");
        scanf("%s",naam);
        
        printf("U bent dus %s %s\n\n", voor, naam);
        
        length=strlen(naam);
        lengte=strlen(voor);
       
        
         printf("In omgekeerde volgorde is dat:");
        for(inhoud2=length;inhoud2>-1;inhoud2--){
         	printf("%c", naam[inhoud2]);
         }
        for(content=lengte;content>-1;content--){
         	printf("%c", voor[content]);
         } 
    	printf("\n");   
       
        
    
    
    }
    this is a program I created to flip your name

    First you have to put in you Firstname than your last name...
    than you get the full name and you get the name in reverse....

    Maybe you could learn something from it
    but it's a very simple program..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  2. Replies: 6
    Last Post: 11-14-2005, 06:23 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. home work help =)
    By pittster in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 09:30 PM