Pointer and String conversion?

This is a discussion on Pointer and String conversion? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> char *IM1, *IM2, *IM3, *IM4, *IM5 ; char IMAGINE[7] = {'I', 'M', 'A', 'G', 'I', ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    Pointer and String conversion?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    char *IM1, *IM2, *IM3, *IM4, *IM5 ;
    char IMAGINE[7] = {'I', 'M', 'A', 'G', 'I', 'N', 'E'};
    
    main()
    {
       clrscr();
       IM1 = IMAGINE;
       IM2 = ++IM1;
       IM3 = &IMAGINE[2];
       IM4 = IM3 + 2 ;
       IM5 = &IMAGINE[4] - 3 ;
    
       printf ("%c\t%c\t%c\t%c\t%c", *IM1,*IM2,*IM3,*IM4,*IM5);
    
    
       getch();
       return 0;
    }
    The program above make use of pointer. I dont understand why IM1 and IM2 is equal to "M" can someone point out why?
    And as for the question below... I dont understand what should I use to get convert string into int . I cant make any sense in doing it

    Write a function called FindAveChar that take in a string parameter str.
    The function returns the average character of all characters in the string. For example, FindAveChar ("ABCDE") returns 'C'.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    Originally posted by Kimimaro
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    char *IM1, *IM2, *IM3, *IM4, *IM5 ;
    char IMAGINE[7] = {'I', 'M', 'A', 'G', 'I', 'N', 'E'};
    
    main()
    {
       clrscr();
       IM1 = IMAGINE;
       IM2 = ++IM1;
       IM3 = &IMAGINE[2];
       IM4 = IM3 + 2 ;
       IM5 = &IMAGINE[4] - 3 ;
    
       printf ("%c\t%c\t%c\t%c\t%c", *IM1,*IM2,*IM3,*IM4,*IM5);
    
    
       getch();
       return 0;
    }
    kimimaro just think of like this
    Code:
    IM1 --> IMAGINE(WHICH IS NOTHING BUT THE LIST ELEMENT OF THE ARRYA)
    SO NOW IM1 IS POINTING TO 'I'
    IM2 <-- ++IM1 (U ARE PRE INCEMENTING THE POINTER IM1 AND NOW IT IS POINTING TO 'M' 
    AND YOU ARE GIVING THE ADDRESS OF IMAGINE[1] THROUGH THE IM1 POINTER JUST BY INCRMENTING IT)
    
    or in can write this like this 
    IM2=&IMAGINE[1] WHICH IS EQUAL TO IM2= ++IM1
    that is why it is printing u 'M' twice

    s.s.harish

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    The answer to your first q is quite simple - you've incremented the pointer IM1 in the line IM2 = ++IM2. This should work:
    Code:
    IM2=IMAGINE;  //This is not a typo
    IM1=IM2++;     //Nor is this
    It would be better to declare your variables inside the main instead of making them global.

    char or int you can still use maths on them the same. The only difference is how big the numbers are that they can represent.

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, I can't give you an answer for the pointer, but basically, think of the char as the same thing as an int. It's just read differently. Take a look at this code.
    Code:
    main()
     {
      int a = 48;
      int b = '0';
      char c = 48;
      char d = '0';
      if(a == b)
        printf("a = b\n");
      if(a == c)
        printf("a = c\n");
      if(a == d)
        printf("a = d\n");
      if(b == c)
        printf("b = c\n");
      if(b == d)
        printf("b = d\n");
      if(c == d)
        printf("c = d\n");
     }
    This program's output would be
    Code:
    a = b
    a = c
    a = d
    b = c
    b = d
    c = d
    That means that each of those four variables have the exact same value. But wait, two of those are chars. How can they be equal. Well, think of it this way.
    Code:
    #define '0' 48
    #define '1' 49
    #define '2' 50
    etc.
    That's how ASCII works. A char is the EXACT SAME as an int, except it only uses eight bits to store its information. Therefore, if you just wrote some code that added the chars together and divided by the number of chars, you would get the average of the characters. It's that simple.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thanks everything makes sense now uhm anyone know how to convert this?

    Write a function called FindAveChar that take in a string parameter str.
    The function returns the average character of all characters in the string. For example, FindAveChar ("ABCDE") returns 'C'.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    won't string be a little different? Thanks for the hardworks but Im still confused with Strings

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Should I use gets or strcpy. But if it wants average, how can I get the numbers of character for that string to be divided?

  8. #8
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Okay, note that woul could have edited your first post to add all of that information.

    gets() is a function that reads characters inputted by the user and keeps reading until it reads that the user has pressed enter and puts them into a character array.

    strcpy() is completely different. It takes one array of characters and copies its contents into another array of characters.

    So, in order to answer your question, I'd have to know what you want to use them for.

    Now, as to your last question, you would access the numbers contained in the string the exact same way you would access the number in an interger. Ignore for a moment the fact that you're dealing with chars. If I told you to write a function to take the average of five intergers, what would the code be. (don't worry about how you would get the five numbers or anything like that. Just write a fuction to take the numbers as arguments and return the average)

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should read the FAQ as to why you should never use gets.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Ahh. Thank you Quzah. I don't personally use gets(), but that's still a very good thing to know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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