Thread: Why isn't this working?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    Why isn't this working?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *names[] = {"Meg", "John", "Susan", "Charlotte", "Mario"};
        unsigned int nelements = sizeof names / sizeof(*names);
    
        while (--nelements > 0)
            printf("%s\n", *names++);
    
        return 0;
    }
    error: '++' needs l-value.

  2. #2
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *names[] = {"Meg", "John", "Susan", "Charlotte", "Mario"};
        unsigned int nelements = sizeof names / sizeof(*names);
    
       int i =0
    
        while (--nelements > 0)
            printf("%s\n", *names[i++]);
    
        return 0;
    }
    that may help.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    I know that works but I wanted to manipulate the pointer directly. What botters me the most is that I took that from the book The C Programming Language 2nd Edition and it is not working

  4. #4
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    if you don't increment the array, you will only get the first item in the array.
    each element of the array has it's own pointer.

    the reason for older books to not work is changes in the language / compilers / standards.

    anything over 2 years old will be problematical to use with current language and compilers.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    names is an array of char *s. You cannot increment an array.

    You can create a pointer, point it at the array, and then increment that. Arrays are not quite pointers. Google the web for the difference between arrays and pointers. (I found this to be helpful.) So, using pointers, I can:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *names[] = {"Meg", "John", "Susan", "Charlotte", "Mario"};
        char **pnames = names;
        unsigned int nelements = sizeof(names) / sizeof(*names);
    
        while (nelements-- > 0)
    		printf("%s\n", *pnames++);
    
        return 0;
    }
    I also put ()'s around the first sizeof, and changed --nelements to nelements-- (otherwise Mario gets left off).
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    It's ok, I don't like him much anyway

    That was exactly the problem... I was trying to increment an array. The authors mention that on the book. I gotta love them and pay more attention. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM