Solved

Easy C question about functions

Posted on 2004-09-09
8
179 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
Comment Utility
Try
void MatchString(char *line);
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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:Kdo
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:FOXBAT
Comment Utility
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:Kdo
Comment Utility

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
Comment Utility
> 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:Kdo
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now