changing normal array to pointer

This is a discussion on changing normal array to pointer within the C Programming forums, part of the General Programming Boards category; I have a question about using pointer. Cuz after i use the pointer, the program isnt work. so i wanna ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    changing normal array to pointer

    I have a question about using pointer. Cuz after i use the pointer, the program isnt work.
    so i wanna change my orginial code by using pointer
    Thank you'

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define merror()   {printf("memory allocation problem\n");exit(1);}
    
    int main(void)
    {
    	char buffer[100];
    	char buf[100][22];
    	char sto[19];
    	int num;
    	int i, j;
    
    
    	printf(" how many names will be entered : \n");
    	fflush(stdout);
    	fgets ( buffer, 10,  stdin );
        num = atoi (buffer);
    
        if (num <=0)
        {
    			printf("empty input \n");
    	}
    	else if (num >100)
    	{
    			printf("input too long \n");
    	}
    	else if (num >0 && num <= 100)
    	{
    			printf("you will enter %d names \n", num);
    	}
    
    
    	for(i=0; i < num; i++)
    	{
    		printf(" enter a name \n");
    		for(j=0; j<21; j++)
    		{
    		      buf[i][j]=fgetc(stdin);
    		      if (buf[i][j]=='\n') {
    		        buf[i][j]='\0';
    		        break;   /* break j-loop */
    		      }
    		    }/* end j-loop */
    
    		    if (j==21)
    		    {
    		      printf("input too long\n");
    		      while(fgetc(stdin)!='\n');
    		      continue;   /* continue i-loop */
                }
       }
    
        printf("\nunsorted input:\n");
        for(i=0; i< num; i++)
        printf("%s\n", buf[i]);
    
    for(i=num; i>0; i--)
        for(j=0; j<i; j++)
          if (strcmp(buf[j],buf[j+1])>0) {
            strcpy(sto,buf[j]);
            strcpy(buf[j],buf[j+1]);
            strcpy(buf[j+1],sto);
          }
    
      printf("sorted input:\n");
      for(i=0; i<=num; i++)
        printf("%s\n",buf[i]);
    
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		for(j=0; j<21; j++)
    		{
    		      buf[i][j]=fgetc(stdin);
    		      if (buf[i][j]=='\n') {
    		        buf[i][j]='\0';
    		        break;   /* break j-loop */
    		      }
    		    }/* end j-loop */
    What's wrong with using fgets() for this?

    You need to post your pointer code if you want any help on that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  4. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 11:58 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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