By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Memory allocation of array accessed using a pointer

P: 1
Hello All,
In the code below when the char *s pointer is declared 4 bytes will be allocated to hold the address of object being pointed by but how much memory is allocated to the string which is an array of character, which is accessed using s and intialised using a loop during runtime?
Thanks in advance:)
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. int main()
  5. {
  6.    char * s,c;
  7.    int i=0;
  8.    while((c=getchar())!='q')
  9.    {
  10.       s[i]=c;i++;
  11.    }
  12.    printf("%s",s);
  13.    getch();
  14. }
Aug 15 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 671
Just a second. You have a pointer s. Where is it pointing to? Try allocating some memory for it.
Aug 15 '07 #2

Expert 10K+
P: 11,448
Hello All,
In the code below when the char *s pointer is declared 4 bytes will be allocated to hold the address of object being pointed by but how much memory is allocated to the string which is an array of character, which is accessed using s and intialised using a loop during runtime?
Say what? Seriously though a char* points to zero or more chars, that's all there
is to it. You shouldn't care about the size of the pointer itself. If the pointer doesn't
point at anything you can't store anything there. It's that simple.

kind regards,

Jos
Aug 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.