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 14-Oct-2009, 10:54
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

Passing values as pointers?


Hello all,
I am new to this forum. and want to thank you for having help for coding.
I am not a mooch. I like to try my hand at my code first. Then get some help.
What I am trying to do is practice pointers to a function.
Below I compiled it but it doesnt display the length of the string?
I want to make sure I am passing the values by pointers to the function.
So that is where I am at. I do understand they have to be the same data
types. Thanks that is a funny emoticon. had to use it
CPP / C++ / C Code:
//trying to test to see if I am passing by pointer?
//am I passing by pointer correctly it doesn't seem to work?

#include <stdio.h>
#include <string.h>
int functionFoo(char *strng, int toAdvance){ 
  int length = strlen(strng);
  return length;
}

int main(){
   int ok = 0;
   char *strng="";
   gets(strng);
   ok = functionFoo(strng, 0);
   printf("%i", ok);                             //dummy me didnt see the missing i next to % fixed
   return 0;
}
Last edited by LuciWiz : 14-Oct-2009 at 12:00. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 14-Oct-2009, 11:28
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

Re: noob..passing values as pointers????


oops found on errror but i sang my song too soon back to debug! but am I passing my pointers right with the char string data type???


but I have another question am I passing pointer correctly in this program??
I am just wondering if I am doing it right? am IT
it works now but just because it works does that mean I am passing pointers into theprogram correctly? thanks
  #3  
Old 14-Oct-2009, 12:00
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

So is this correct way to pass pointers?????--easy question


hey gues I got it to work but is this the correct way to pass pointers?
I am just making sure I am passing the pointers correctly and initializing
the char string correctly????


CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
int foo(char *strng){
    
    int length = strlen(strng);
  
    return length;

}//end of foo()

int main(){
   int ok = 0;
   char *strng="";
   gets(strng);
   ok = foo(strng);
   printf("Number or character is: %i", ok);
   return 0;
}




Last edited by LuciWiz : 14-Oct-2009 at 12:22. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #4  
Old 14-Oct-2009, 13:01
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: noob..passing values as pointers????


You have one big error:

CPP / C++ / C Code:
char *strng="";
This statement allocates an empty string (only enough space for the null terminating character).

If you wish to store anything into the string, you should allocate it like this:
CPP / C++ / C Code:
char strng[32]="";

This would now allow you to enter 31 characters into the string.
  #5  
Old 14-Oct-2009, 13:30
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

Re: noob..passing values as pointers????


hey fakepoo

that name makes me laugh!
hey thanks, I was thinking that but hey thanks a lot! for you taking the time to help me..I appreciate your expert advice..

so yu think I am passing with pointers correctly do you?
  #6  
Old 14-Oct-2009, 13:35
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: noob..passing values as pointers????


Here is a general tutorial on pointers. It may answer some of your questions. If you have specific questions, feel free to post them here and we'll do our best to answer them.
  #7  
Old 14-Oct-2009, 13:47
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

Re: noob..passing values as pointers????


hey fakepoo thanks for the pointer ref! great

hey look at this it gives me some kind of indirection error? something about char * ??I don't know what that means? compile and run and maybe you can see what I mean? thanks

Code:
#include <stdio.h> #include <string.h> int foo(char *strng){ int length = strlen(strng); return length; }//end of foo() int main(){ int ok = 0; char *strng[32]={""}; gets(strng); ok = foo(strng); printf("Number or character is: %i", ok); return 0; }
  #8  
Old 14-Oct-2009, 13:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: noob..passing values as pointers????


CPP / C++ / C Code:
char *string1 = "One way to declare a string.";
char string2[64] = "Another way to declare a string.";

int length1 = strlen(string1);
int length2 = strlen(string2);
Notice that you can either declare a char pointer (char *) or a char array (char[]). Both can be accessed and used as a char *.
  #9  
Old 14-Oct-2009, 14:05
tbites tbites is offline
New Member
 
Join Date: Oct 2009
Location: USA
Posts: 7
tbites is on a distinguished road

Re: noob..passing values as pointers????


wow I tried it in mine and it works; wow man i feel so dumb
thanks so much for the explaination and your valuable time
I hope I am as good as you some day -thanks fakepoo!

so array can be used with char* since arrays are consider pointers??? is that right?
  #10  
Old 14-Oct-2009, 14:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: noob..passing values as pointers????


Quote:
Originally Posted by tbites
so array can be used with char* since arrays are consider pointers??? is that right?
Look at the section "Pointers and arrays" on this page. It does a very good job of explaining it.
 
 

Recent GIDBlogOnce again, no time for hobbies 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
Functions: Passing pointers vs by reference dlp C++ Forum 0 05-Nov-2007 04:36
Sort an array of structures sassy99 C Programming Language 10 15-Feb-2007 08:46
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

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

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


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