Thread: Types problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    24

    Types problem

    Code:
    void Prints(char **menu)
    {
       while(**menu)
       {
    
            printf("%s  ", *(*(menu)+i));
    
        }
    }
    giving me an error in printf:
    Unhandled exception at 0x583814cf (msvcr100d.dll) 0xC0000005: Access violation reading location 0x00000066.

    problem comes from **menu I am sure but I dont know how to fix it ? What is the type if **menu ???

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A **variable is a pointer to a pointer to variable, and I doubt that is what you want to print your menu.

    If menu is a string (char's WITH an end of string marker on the end of them: '\0'), then you can print that string with just printf("%s ", menu);

    And Welcome to the forum, Westony!

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    24
    Tnx for the welcome but i have
    Code:
    char *menu[] = {"data", "comes", "here"};
    i know how to print it pointer by pointer but i wanna try to print it symbol by symbol

    i for get one i++ but this does not matter still giving me the error and and still **menu what TYPE OF DATA is it ???

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by westony View Post
    Tnx for the welcome but i have
    Code:
    char *menu[] = {"data", "comes", "here"};
    i know how to print it pointer by pointer but i wanna try to print it symbol by symbol

    i for get one i++ but this does not matter still giving me the error and and still **menu what TYPE OF DATA is it ???
    Try:

    Code:
    for(i=0;i<3;i++)
      printf("\n%s", menu[i]);
    
    Or:
    
    for(i=0;i<SIZE of menu; i++)
      printf("%c", menu[i]
    
    But you'll have to tell SIZE of menu, how many char's you want to print. Once you go into another function, it needs that info.
    
    
    
    Just a call: 
    myFunction(menu);
    
    and then the function, itself:
    void myFunction(char *menu)
    **menu is a pointer to a pointer to your menu.
    Last edited by Adak; 07-23-2010 at 02:14 AM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    24
    ok but isn't this printf("%c", menu[i]) join to print them pointer by pointer data comes here!!!



    i wanna print it symbol my symbol d a t a c o m e s h e r e ????

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, menu points to the first element of an array of pointers to char, where each pointer to char points to the first character of a string. You want to print these strings.

    Since there is no way to determine the number of menu items given just this menu pointer, you should pass the number of items as an argument, and thus be able to write say:
    Code:
    void printMenu(char **menu, size_t num_items)
    {
        size_t i;
        for (i = 0; i < num_items; ++i)
        {
            printf("%s ", menu[i]);
        }
    }
    Incidentally, you probably should have written instead:
    Code:
    const char *menu[] = {"data", "comes", "here"};
    since string literals are not to be modified.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wonder if this is what he was looking for?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j, n; 
      const char *menu[]={"data", "comes", "here"};
    
    
      printf("\n\n");
    
    int main() {
      int i, j, len, n; 
      char *menu[]={"data", "comes", "here"};
    
    
      printf("\n\n");
      for(i=0;i<sizeof(menu)/sizeof(menu[0]);i++) {
        len=strlen(menu[i]);
        for(j=0;j<len;j++) {
          printf("%c ", menu[i][j]);
        }
      }
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar(); ++i;
      return 0;
    }
    Output is: d a t a c o m e s h e r e

    Edit: Thanks, Laserlight! All fixed!
    Last edited by Adak; 07-23-2010 at 04:02 AM.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    24
    If I read right there is no way to do it by my way ???
    Because there is no way to determine the number of menu

    and can you help me please understand the meaning of this size_t because in could not understood it in MSDN

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We've shown you two ways to print it out. You said you knew another way with pointers.

    Seems to cover the subject, no?

    No, not quite that way, but you can print it out with pointers, instead of using an index, of course.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by westony
    Because there is no way to determine the number of menu
    Of course, given the array, you can indeed determine the number of menu items, whether by counting manually or by using sizeof(menu) / sizeof(menu[0]). The problem is that in the function, you only have a pointer to a pointer, thus using sizeof will not work to find the number of menu items. Hence, you pass the number of items as an argument.

    Quote Originally Posted by westony
    and can you help me please understand the meaning of this size_t because in could not understood it in MSDN
    It is just an unsigned integer type commonly used for values that represent sizes in the C standard library. It is also the type of the result of the sizeof operator.

    Adak's example looks like it might be what you have in mind, but of course you should fix the presumably deliberate bug and move the nested loops into a function as you desire. In the process of fixing that bug you would then be passing the size as an argument. You also might want to cache the result of strlen instead of calling it on every iteration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You mean the int instead of size_t? (I confess, I seldom use size_t for anything).

    I ran the program - ran fine. I was in a hurry though. Just an hour before the Tour de France live coverage starts.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    You mean the int instead of size_t?
    No, the use of sizeof(menu) instead of sizeof(menu) / sizeof(menu[0]).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    24
    Quote Originally Posted by Adak View Post
    I wonder if this is what he was looking for?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j, n; 
      const char *menu[]={"data", "comes", "here"};
    
    
      printf("\n\n");
      for(i=0;i<sizeof(menu);i++)
        for(j=0;j<strlen(menu[i]);j++) {
          printf("%c ", menu[i][j]);
        }
    
      printf("\n\n\t\t\t     press enter when ready");
    
      i = getchar();
      return 0;
    }
    Output is: d a t a c o m e s h e r e
    my VS shows me again that error :@ ;
    Unhandled exception at 0x5805d540 (msvcr100d.dll) in TestingZone.exe: 0xC0000005: Access violation reading location 0xcccccccc.

    My syntax is the same but with pointers!!!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    westony: read my comments about Adak's (now-fixed) bug.
    Last edited by laserlight; 07-23-2010 at 04:03 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That was just my flub - thanks, and it's fixed now.

    Edit: Sorry, Westony! Get the updated version of that post!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM
  5. having a problem with types
    By Donn1 in forum C Programming
    Replies: 8
    Last Post: 10-31-2001, 08:10 PM