pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; im doing a little practice on pointers and am trying to make a program that uses arrays and pointers. it ...

  1. #1
    esi
    esi is offline
    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,682
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    esi
    esi is offline
    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, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21