1

Can anyone help me passing char array to method when i tried it is only copying one index value so

char c1[]={0x01};  
char c2[]={0x02};
char c3[]={0x03};
char *c[3];
c[0] = c1;
c[1] = c2;
c[2] = c3;//if i pass this char array to the below method only c[0] is copied 
 char* arrrr =[self mountLVparams:NULL :c :code_ward_arr];    
 //my method being this
-(char *)mountLVparams:(signed char *)initData :(char *)obj :(signed char *)codeWard

4 Answers 4

1

c is a pointer to a pointer. your method signature should be like -(char *)mountLVparams:(signed char *)initData :(char **)obj :(signed char *)codeWard

char c1[]={0x01};  
char c2[]={0x02};
char c3[]={0x03};
char *c[3];
c[0] = c1;
c[1] = c2;
c[2] = c3;//if i pass this char array to the below method only c[0] is copied 
 char* arrrr =[self mountLVparams:NULL :c :code_ward_arr];     

-(char *)mountLVparams:(signed char *)initData :(char **)obj :(signed char *)codeWard
{
    int i;
    for(i=0;i<3;++i)
        printf("%d----%c", i,*obj[i]);

}
Sign up to request clarification or add additional context in comments.

2 Comments

still i m not able to retieve the value is their any method lik -(char *)mountLVparams:(signed char *)initData :(char **)obj[3] :(signed char *)codeWardlike this when i call this method i m getting Incompatible pointer types sending 'char *[3]' to parameter of type 'char *' this warning
try this -(void)MethodA { char c1[]={"A"}; char c2[]={"B"}; char c3[]={"C"}; char *c[3]; c[0] = c1; c[1] = c2; c[2] = c3; [self mountLVparams :c] ; } -(void)mountLVparams:(char **)obj { int i; for(i=0;i<3;++i) printf("%d----%c", i,*obj[i]); }
1

I have one solution by passing array to your function and then creating char *

    const char c1[]={0x01};  
    NSString *s1 = [[NSString alloc] initWithCString:c1 encoding:NSUTF8StringEncoding]; //convert into string 

    const char c2[]={0x02};
    NSString *s2 = [[NSString alloc] initWithCString:c2 encoding:NSUTF8StringEncoding]; //convert into string 

    const char c3[]={0x03};
    NSString *s3 = [[NSString alloc] initWithCString:c3 encoding:NSUTF8StringEncoding]; //convert into string 

    NSArray *arr = [NSArray arrayWithObjects:s1,s2,s3,nil]; // adding all strings 
    [s1 release];
    [s2 release];
    [s3 release];

Now function would be like this where u will pass arr(NSArray):

-(char *)mountLVparams:(signed char *)initData :(NSArray *)arrChars :(signed char *)codeWard
{

int count = [arrChars count];
char *cargs = (char *) malloc(sizeof(char) * (count + 1));
//cargs is a pointer to 4 pointers to char

int i;
for(i = 0; i < count; i++) {
    NSString *s = [arrChars objectAtIndex:i];//get a NSString
    const char *cstr = [s cStringUsingEncoding:NSUTF8StringEncoding];//get cstring
    int len = strlen(cstr);//get its length
    char *cstr_copy = (char *) malloc(sizeof(char) * (len + 1));//allocate memory, + 1 for ending '\0'
    strcpy(cstr_copy, cstr);//make a copy
    cargs[i] = cstr_copy;//put the point in cargs
}
cargs[i] = NULL;
NSLog(@"%c | %c| %c ",cargs[0],cargs[1],cargs[2]);
return cargs;
}

Credit for creation of char from array of string goes to @yehnan follow objective-c nsarray to c array

Use like this:

  char *cs = [self mountLVparams:(your arguments here and pass array here)];
    NSLog(@"%c | %c | %c",cs[0],cs[1],cs[2]);

Comments

1

perhaps, you would be looking for the following where you can pass the NSString object with the desired content as parameter:

NSString *_string = [NSString stringWithString:@"\x01\x02\x03"];
[self yourMethod:_string];

and inside the -yourMethod:

- (void)yourMethod:(NSString *)stringWithChars {
    char _ch = [stringWithChars characterAtIndex:1]; // it gives back you an unsigned short but in this case you can use this value as char without any problem
    NSLog(@"chat at index #1 : %d, %c", _ch, _ch); // or do whatever you'd like to do.
}

Comments

0

parameter c is the pointer pointing to your character array. The pointer will point the first address of the array hence it is by default c[0].

You can pass it as NSstring. [NSString stringWithFormat:%s,c].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.