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. Question!

Question!

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++data-structures
7 Posts 6 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.
  • R Offline
    R Offline
    Razanust
    wrote on last edited by
    #1

    My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }

    N T S D 4 Replies Last reply
    0
    • R Razanust

      My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      Razanust wrote:

      But the 'sizeof' command is returning the value '1'

      It returns 1 and that is correct, because this is the size of a char type. If you need the count of the elements in array, you have to use _countof macro -> here.[^] This form here: count = _countof(ch) will always return 100, because you actualy allocated memory for 100 elements on the stack. But note that the variable "n" from your code holds the count of the characters. ;)

      Razanust wrote:

      count=sizeof(ch[100]);

      Also note that your use of sizeof is wrong. If you need the size of an array in bytes, than you must change it to: sizeof(ch). Best wishes! :) Nuri

      modified on Tuesday, June 30, 2009 3:07 PM

      1 Reply Last reply
      0
      • R Razanust

        My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }

        T Offline
        T Offline
        thebeekeeper
        wrote on last edited by
        #3

        If by 'whatever input' you mean the number you put in the square brackets for ch[100], I think this is because ch[n] is always going to be a pointer. It's equivalent to char *ch = (char*)malloc(sizeof(char) * n); then sizeof(ch) will always be 1 because it's pointing to the start of your array. In C, you sort of always have to keep track of the size of your arrays manually.

        L 1 Reply Last reply
        0
        • R Razanust

          My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          OK - that's not how sizeof works - it tells you the size of a type, either via the typename ('sizeof (char)') or a compile-time static expression ('sizeof (3 * 4.0)'). The WIkipedia entry[^] is quite accurate. I presume you know you can use n to tell you how many characters have been entered...

          Razanust wrote:

          printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; }

          Can I suggest you read up how strings (specifically null terminated strings) and arrays work in C?

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • T thebeekeeper

            If by 'whatever input' you mean the number you put in the square brackets for ch[100], I think this is because ch[n] is always going to be a pointer. It's equivalent to char *ch = (char*)malloc(sizeof(char) * n); then sizeof(ch) will always be 1 because it's pointing to the start of your array. In C, you sort of always have to keep track of the size of your arrays manually.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            thebeekeeper wrote:

            because ch[n] is always going to be a pointer

            Nope. ch is a pointer, ch[n] is one character out of an array of characters, that is why its size is 1. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


            T 1 Reply Last reply
            0
            • R Razanust

              My object is to fill the buffer first and then make it empty. But the 'sizeof' command is returning the value '1', whatever input i give to it. Why? The code is mentioned below. #include<stdio.h> #include<conio.h> main() { char dec; static char ch[100]; int n=0,i,p,c,count; while(1) { ch[n]=getche(); n=n+1; if ( ch[n-1]=='\r') break; } count=sizeof(ch[100]); printf("%ld",count); printf("Your name is"); for (c=0;c<count;c++) { printf(" %c",ch[c]);} printf("\n\nWant to delete the array?"); dec=getche(); if (dec=='y') { for (i=0;i<99;i++) ch[i]=0; } if (dec=='y') { for(p=0;p<99;p++) printf("%c",ch[p]); } }

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Razanust wrote:

              count=sizeof(ch[100]);

              As a side note, your code (possibly) does not own the memory at location ch[100]. While that variable holds 100 chars, they are numbered 0-99.

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              1 Reply Last reply
              0
              • L Luc Pattyn

                thebeekeeper wrote:

                because ch[n] is always going to be a pointer

                Nope. ch is a pointer, ch[n] is one character out of an array of characters, that is why its size is 1. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                T Offline
                T Offline
                thebeekeeper
                wrote on last edited by
                #7

                ha! I forgot that [] are for indexing into arrays!

                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