Solved

Easy C question about functions

Posted on 2004-09-09
8
182 Views
Last Modified: 2010-04-15
I have the following function that formats a string and inserts it into a data structure.

char line[256];

{
  int c = 0; int i = 0;                            

  while ((c = getchar()) != '\n' )
    if (i < max)
      line[i++] = c;
            else
        printf("\n You cannot input more than 255 chars \n");
            line[i] = '\0';  // terminate line with null
      
  // -------->        MatchString(line);      an example of  something that i want to do

         if ((ptr = malloc(i + 1)) != NULL)
             sl = listInsert(sl, strcpy(ptr,line)); // load data



i want to make a function that will accept the line  so i can perform operations on the line and other data, for example...

void MatchString(char line[])
{   printf("%S contents = ", line); }

Can any one write a function /header that will allow to me input the line


Thanx in advance.
      


0
Comment
Question by:FOXBAT
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12019124
Try
void MatchString(char *line);
0
 
LVL 30

Expert Comment

by:Axter
ID: 12019134
void MatchString(char *line)
{   printf("%S contents = ", line); }

If the function does not modify the argument, then you should make it constant.

void MatchString(const char *line) //const char* type
{   printf("%S contents = ", line); }
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 12019475
Hi Foxbat,

> i want to make a function that will accept the line  so i can perform operations on the line and other data, for example...


There are already a lot of functions around for getting a string .

gets() and fgets() come to mind (though you should avoid gets()).


Kent
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 30

Expert Comment

by:Axter
ID: 12019557
>>Can any one write a function /header that will allow to me input the line

You can use scanf to input data into the string.

void GetString(char *line)
{  
     printf("enter data: ");
     scanf("%s", line);
}
0
 

Author Comment

by:FOXBAT
ID: 12022567
ok so if i make a glibal pointer

char *temp;

{
....
line[i] = '\0'; // after string is finshed
then i try copy the string to the pointer, but i get an error
strcpy(temp, line);

}
note: that i need to use the string input the way it is being done at the moment, i cant use gets();

and all i want to do in the function is compare if the string matches another string

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 12022582

temp is simply a pointer.  What's it pointing to?

line[i] = 0;   // terminate the line
temp = line; // temp now points to line.  You can use temp to peruse the data.



Kent
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 12024716
> while ((c = getchar()) != '\n' )
>    if (i < max)
>      line[i++] = c;
>          else
>       printf("\n You cannot input more than 255 chars \n");
>          line[i] = '\0';  // terminate line with null


can be replaced with

char line[256];
fgets ( line, 256, stdin );

Alternatively, there is a readline library which may be of some interest to you
http://cnswww.cns.cwru.edu/~chet/readline/rltop.html


Welcome back Kent ... Nice to see you again
:)
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 12025470
Hi Sunny,

Nice to be back, too.  Though my new duties are keeping me wayyyyy too busy.  I'll likely not be as prolific for a while, but as you noticed, I AM back!

Kent
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question