GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-May-2004, 03:32
mgdpetter mgdpetter is offline
New Member
 
Join Date: May 2004
Location: Sydney
Posts: 6
mgdpetter is on a distinguished road

Passing strings in functions..HELP please...


I am new to programming and need some help,
I want to pass a string into a function that uses strtok(), or something else, to check for spaces or tabs and then return the first non-space-containing-word to main...
This seems to be an impossible task.
My code looks like this:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
void function( char ar1[],char ar2[],int n);
int
main()
{

    int i=8;
    char test_string[50] = "some text	with spaces";
    char   result[8];
   function(test_string, result, i);
   printf("%s\n", result);

    return 0;
}


void
function  (char ar1[],
              char ar2[],  
                int       n)       
{

        ar2 = strtok(ar1, " \n\t");
}

It works if i write
printf("%s", strtok(test_string, " \t\n"));
in main, Why doesnt it work in the function??
thanx
Last edited by dsmith : 24-May-2004 at 08:21. Reason: Please use [c] & [/c] to highlight code
  #2  
Old 24-May-2004, 04:30
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Hi mgdpetter, welcome to GID Forums, I think you should read this first C++ Tags it shows how to post your code for better looks and readablity..

I'm not exactly sure why your code doesn't work.What I see is that strtok return a char * and I made the functin to return the char*:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

char* function( char ar1[],int n);
int
main()
{

int i=8;
char test_string[50] = "some text with spaces";
char result[8];

char* r=function(test_string, i);
printf("%s\n", r);


return 0;
}


char*
function (char ar1[],int n) 
{

return strtok(ar1," \t\n");
}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 24-May-2004, 23:02
mgdpetter mgdpetter is offline
New Member
 
Join Date: May 2004
Location: Sydney
Posts: 6
mgdpetter is on a distinguished road
Thanx man. Now it works.
you're a legend.
I'll use the C tags in the future..
Thanx again.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Including Maps and strings?? maddie C++ Forum 17 05-Jul-2004 07:25
Help on passing in arrays in functions? nusstu C Programming Language 10 02-Apr-2004 11:42
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 22:23
New to C...need help calling functions rbeierle C Programming Language 3 10-Feb-2004 19:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:58.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.