[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Easy C question about functions

Posted on 2004-09-09
8
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 46

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
Industry Leaders: 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 46

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 400 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 46

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

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!

Question has a verified solution.

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

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.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

656 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