Thread: how to print a vector?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Italy
    Posts
    6

    how to print a vector?

    Hi, today I started studying vectors and here is my first elementar question:
    how to print a vector????
    I have this vector:


    Code:
     char vocali[]= {'a','e','i','o','u'};
    I want to print it on screen.

    the following code give me the value 0.

    Code:
    								   			 printf( "\n\n\nIl vettore vocali e'----> %c\n\n\n", &vocali );
    What's wrong? Thanks!
    [COLOR=#fafafa !important]

    [/COLOR]
    http://www.google.com/uds/css/small-logo.png

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First, don't use the address of operator (&) with printf (unless you want to print an address, not the value at the address).

    Second, vocali is a char array, and %c is a single char. Maybe you want:

    Code:
    printf( "\n\n\nIl vettore vocali e'----> %c\n\n\n", *vocali );
    * dereferences the pointer and gives you the same thing as vocali[0] would. Or

    Code:
    printf( "\n\n\nIl vettore vocali e'----> %s\n\n\n", vocali );
    Which prints a string. However, because of the way vocali is declared, it is not null terminated, so you may get garbage characters at the end.

    To properly print out this array, you would need something along the lines of:

    Code:
    	int i, len = sizeof(vocali); 
    	for (i = 0; i < len; i++) {
    		putchar(vocali[i]);
    	}
    Notice that is sizeof() and not strlen(), since vocali is not null terminated but is of fixed size.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    fwrite(vocali,sizeof vocali,1,stdout);fflush(stdout);
    Better you use the standard behaviour for strings in C like

    Code:
    char vocali[]="aeiou";
    puts( vocali );

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Italy
    Posts
    6
    Thanks for the help guys, but I have not studied things like "%s" or putchar or "fwrite" or "fflush" or "sizeof" or "puts".

    Can this be done in other "basic" way? Thanks.
    undefined

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,749
    Quote Originally Posted by MK27 View Post
    Code:
    	int i, len = sizeof(vocali); 
    	for (i = 0; i < len; i++) {
    		putchar(vocali[i]);
    	}
    You can also do it this way; it is not as good as MK27 code

    Code:
            char vocali[]= {'a','e','i','o','u'};
    	int i, len = 5; 
    	for (i = 0; i < len; i++) {
    		printf("%c", vocali[i]);
    	}
            printf("\n");
    or this way
    Code:
            char vocali[]= {"aeiou"};
            printf("%s\n", vocali);
    Note: None of these code samples are using vectors; some are using char arrays (these might be considered vectors; but are not normally considered that by programmers).

    Tim S.
    Last edited by stahta01; 10-26-2011 at 02:04 PM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Italy
    Posts
    6
    Quote Originally Posted by stahta01 View Post
    You can also do it this way; it is not as good as MK27 code

    Code:
            char vocali[]= {'a','e','i','o','u'};
        int i, len = 5; 
        for (i = 0; i < len; i++) {
            printf("%c", vocali[i]);
        }
            printf("\n");
    or this way
    Code:
            char vocali[]= {"aeiou"};
            printf("%s\n", vocali);
    Note: None of these code samples are using vectors; some are using char arrays (these might be considered vectors; but are not normally considered that by programmers).

    Tim S.
    Thanks, perfect

    We call monodimensional table (one column) vectors

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    how come when i run this with percent i instead of c i get the same addresses

    Code:
    printf( "\n\n\nIl vettore vocali e'----> %i\n\n\n", &vocali );
    printf( "\n\n\nIl vettore vocali e'----> %i\n\n\n", vocali );
    isnt the first one the address of the pointer, and the second one the address of where the pointer is pointing too, which in this case would be the address of vocali?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,749
    @camel-man: %p is used to printf pointers.

    Tim S.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    ok so what exactly is this address referring to? is it just the address of vocali?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    3,749
    Quote Originally Posted by camel-man View Post
    ok so what exactly is this address referring to? is it just the address of vocali?
    When you DO something that is NOT defined it really DOES NOT matter what it refers to!!

    You told it to print an integer NOT a pointer this is an undefined operation.

    Edit: Just ran it myself and MinGW GCC ignores the "&" in front of a array name; even when used with "%p"

    But, this works as expected.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char vocali[]= {"aeiou"};
        char * pV = vocali;
    
        if (&vocali == vocali)
        {
           printf("Same Value\n");
        }
    
        printf( "address of pV %p\n", &pV );
        printf( "value of %p\n", pV );
    
        return 0;
    }
    Note: As I was once told, an array name is NOT the same as a pointer; it is similar a pointer.

    Tim S.
    Last edited by stahta01; 10-26-2011 at 07:44 PM. Reason: added note

  11. #11
    Registered User mdj441's Avatar
    Join Date
    Aug 2011
    Location
    NYS, USA
    Posts
    18
    The important difference is that &vocali is a char(*)[6] (ie. "pointer to array 6 of char"). So it is actually a completely different type than char*.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print/access the 1st element of the first set of the vector
    By kapil1089thekin in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 02:28 PM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. Trying to print toString from object held in vector
    By whiskedaway in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2009, 06:13 AM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 06:47 PM