LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2003, 07:56 AM   #1
chens_83
Member
 
Registered: Dec 2001
Location: Adelaide , South Australia
Distribution: redhat 7.2, Debian , OpenBSD
Posts: 123

Rep: Reputation: 15
why/where to point in C program


ok i understand this program up until the point where i need to have a guard for a do-while

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
char str[80];
FILE *fp;   

fp = fopen("TEST.txt","w"); 
							
	if(fp==NULL)
	{
		printf("CAnnot open file.\n"); 
		exit(1);
	}

	do
	{
		printf("Enter a String (Enter to quit) : ");
		gets(str);
		strcat(str,"\n"); 
		fputs(str,fp); 
	}while(*str != '\n');  //why dereference?

return 0;
}
I am confused to why i need to point at the while's guard?
can some one please push me in the right direction, or explain why.

-Chensy in a frenzy
 
Old 01-29-2003, 10:10 AM   #2
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
*str is the reference to the first element of the array "str" which is str[0].
If a user didn't enter anything and just press enter, the str[0] would be '\n' and the program will exit.
So the purpose is to exit the program when the user press "Enter".
*str != '\n' is the same as str[0] != '\n'

Last edited by moeminhtun; 01-29-2003 at 10:35 AM.
 
Old 01-29-2003, 11:17 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
You can condense it like this:
Code:
#include<stdio.h>

int main()
{
  FILE *fptr = NULL;
  char buff[255];

  fptr = fopen("myfile","w");

  do
    {
      fgets(buff, 255, stdin);
      fputs(buff, fptr);
    }while (*buff != '\n');


  fclose(fptr);
  return 0;
}
 
Old 01-29-2003, 05:47 PM   #4
chens_83
Member
 
Registered: Dec 2001
Location: Adelaide , South Australia
Distribution: redhat 7.2, Debian , OpenBSD
Posts: 123

Original Poster
Rep: Reputation: 15
Ok . Thanks for the help. Ok that reference was a little confusing, i would of just used str[0]. Is the pointer way that much quicker instead of using the str[0] ?
 
Old 01-29-2003, 07:28 PM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
The array decays into a pointer. It's not going to make a difference in speed though.
 
Old 01-30-2003, 01:50 PM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
It's useful to know that:
Code:
array[x]
is just syntactic sugar for:
Code:
*(array + x)
In the case of your program, x was 0, so *(array + x) went to *array.

Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anyone point me to a good All in one IM program? BlackDogg Linux - Software 13 02-16-2006 03:21 PM
Point to point connectivity between 2 pcs shroffsp Linux - Networking 1 09-22-2003 08:57 AM
Mandrake MNF with point-to-point T1, routers ioannes Linux - Networking 0 07-24-2003 08:59 AM
Is there a detailed point by point comparison on Linux to Windows? Paul Parr Linux - General 4 04-26-2003 02:35 AM
point to point address assignment of ppp0 andyn Linux - Networking 0 10-11-2002 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration