• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Basic C Language question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Basic C Language question


  • Subject: Basic C Language question
  • From: Isaac Rivera <email@hidden>
  • Date: Sun, 8 Aug 2004 16:12:19 -0400

Xcoders:

Newbie trying to learn C here.
Why wont this code behave as expected?

<SNIPET>
#include <stdio.h>
#include <stdlib.h>
// Length of the input buffer:
#define BUFFER_LEN 10
// Maximum number of strings:
#define NUM_P 10

int main () {
	// Input buffer:
	char buffer[BUFFER_LEN];
	// Array of string pointers:
	char *ps[NUM_P] = { NULL };
	// Pointer to the buffer:
	char *pBuffer = buffer;
	// Iterators:
	int i = 0, j = 0;

	for(i = 0; i < NUM_P; i++)
	{
		// Set pointer to the beginning of buffer;
		pBuffer = buffer;
		// Prompt for input:
		printf("\nEnter %s message, or press \"ENTER\" to end:\n", i > 0 ?
"another" : "a");
		// Read a string of up to BUFFER_LEN characters:
		while(((pBuffer - buffer) < BUFFER_LEN - 1) && ((*pBuffer++ =
getchar()) != '\n'));
</SNIPET>

Shouldn't the last line of code kill the while loop when one types 9
characters in a row?

But it doesn't it keeps on taking imput as long as I don't press ENTER.

Why is this?

Isaac Rivera
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Basic C Language question
      • From: Eric Albert <email@hidden>
References: 
 >Basic questions (From: Shawn Somers <email@hidden>)

  • Prev by Date: Re: Xcode usability (was Re: Where did the GDB drawer go?)
  • Next by Date: Re: using an external library in an Xcode project
  • Previous by thread: Re: Basic questions
  • Next by thread: Re: Basic C Language question
  • Index(es):
    • Date
    • Thread