cocoa - NSString pointer passed to function... not keeping the value I set -
i'm not sure i'm doing wrong here. i've tried setting s1..3
in foo
using:
s1 = [[nsstring alloc] initwithstring:[filepaths objectatindex:0]];
context below:
void foo(nsstring *s1, nsstring *s2, nsstring *s3){ //assign long string nsstring *fps //... //break fps smaller bits nsarray *filepaths = [fps componentsseparatedbystring:@"\n"]; //the above worked! let's assign them pointers s1 = [filepaths objectatindex:0]; //repeat s2 , s3 nslog(@"%@",s1); //it worked! we're done in function } int main(int argc, const char * argv[]){ nsstring *s1 = nil; //s2 , s3 foo(s1,s2,s3); //this should work nslog(@"%@",s1); //uh oh, null! return 0; }
no.
you passing in pointers objects can mutated locally. not changing original objects, might think plain c.
if want use method (which not recommend - it's odd see in cocoa except in case of nserror
), have like:
void foo(nsstring **s1, nsstring **s2, nsstring **s3) { *s1 = [filepaths objectatindex:0]; // etc. }
you pass in &s1
argument.
this will, of course, clobber whatever in s1
, potentially cause memory leaks, thread unsafety, etc., unless careful. why won't this.
Comments
Post a Comment