Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. array and pointer

array and pointer

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonygeeker
    wrote on last edited by
    #1

    I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?

    R CPalliniC C 3 Replies Last reply
    0
    • A Anonygeeker

      I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      The difference is in the prototypes : void print(int *a) vs void print(int *a[]) The first takes a pointer to integer. The second takes an array of pointers to integers. To make the second function work you have to dereference a pointer : printf( "%d\n", *a[i] );

      1 Reply Last reply
      0
      • A Anonygeeker

        I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        the equivalent of

        void print(int *a)

        with array notation is

        void print(int a[])

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • A Anonygeeker

          I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?

          C Offline
          C Offline
          cao_aba3
          wrote on last edited by
          #4

          void print(int *a[]): It looks like a 2 dimensional array I think so. ;)

          L 1 Reply Last reply
          0
          • C cao_aba3

            void print(int *a[]): It looks like a 2 dimensional array I think so. ;)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            No, it's a one-dimensional array of pointers.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups