Thread: pointers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    pointers

    im doing a little practice on pointers and am trying to make a program that uses arrays and pointers. it takes a character input from user and then gives us the character at a specific array we input. but its not working and doesnt even letme type the number of characters i want.
    the source code i tried is as follows..

    Code:
    #include <stdio.h>
    int main()
    {
    	int l,b;
    	int i=0;
    	char *p;
    	char a[i];
    	p=a;
    		printf("enter the length of string:\n");
    		scanf("%d",&l);
    		printf("enter string:\n");
    		for (i=0;i<l;i++)
    		{
    			scanf("%c",&a[i]);
    		}
    		printf("enter the cell adress:\n");
    		scanf("%d",&b);
    
    		printf("%c",*(a+b));
    	return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int l,b;
    int i=0;
    char *p;
    char a[i];
    while declaring an array u should specify its size in before hand. More over it had to be const value, but not like this

    Code:
    char a[i];   // check your i value which is 0
    change your to
    Code:
    char a[80];   // some size
    Specify its size so that it is const.

    Code:
    printf("%c",*(p+b));
    ssharish2005

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And of course you should check that size entered by the user and used in the loop:
    for (i=0;i<l;i++)
    is not greater when the size of your array
    char a[80]; // some size
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Dynamically allocating a string is yet another possibility with all the flexibility you ought to need here.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by citizen
    Dynamically allocating a string is yet another possibility with all the flexibility you ought to need here.
    He is studying arrays and pointers, not dynamic memory allocation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right, because dynamic memory allocation has nothing to do with pointers, and therefore is completely unrelated!


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    18
    thnx a lot... really helped a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM