Solved

Easy C question about functions

Posted on 2004-09-09
8
183 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 138
How to align numbers in C using the %d 2 112
C++ finding a sting in a char* string from a text file 3 130
An API detour question 7 103
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

730 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