Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Easy C question about functions

Posted on 2004-09-09
8
Medium Priority
?
187 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 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