Why am I getting:
this error control reaches end of non-void function [-Wreturn-type]
#import <Foundation/Foundation.h>
const int MAX = 4;
int main (int argc, const char * argv[])
{
char names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",};
char *pointer[MAX];
for(int i=0; i<MAX; i++)
{
pointer[i] = &names[i];
}
for (i = 0; i < MAX; i++)
{
NSLog(#"Value of var[%d] = %s\n", i, *pointer[i] );
}
return 0;
}
A vector of chars is not the same as a vector of strings. A char has space for just one character. char[] is a vector of characters, not a vector of strings. If you initialise it like this:
char names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",};
The names variable will keep the pointer just to the first string, it will be initialised just with the first string. All the next strings will be ignored.
I think you are trying to do something like this:
#import <Foundation/Foundation.h>
const int MAX = 4;
int main (int argc, const char * argv[])
{
NSString *names[] = {
#"Zara Ali",
#"Hina Ali",
#"Nuha Ali",
#"Sara Ali",};
NSString *pointer[MAX];
for(int i=0; i<MAX; i++)
{
pointer[i] = names[i];
}
for (int i = 0; i < MAX; i++)
{
NSLog(#"Value of var[%d] = %#\n", i, pointer[i] );
}
return 0;
}
Related
I have NSMutableData "30352514 38001300 00000001"
i need convert byte to bit
00110000001101
and that to NSString
Thx
Use this for bytes:
const char *byte = [data bytes];
NSLog(#"%s",byte);
This is for bits:
const char *byte = [data bytes];
unsigned int length = [data length];
for (int i=0; i<length; i++) {
char n = byte[i];
char buffer[9];
buffer[8] = 0; //for null
int j = 8;
while(j > 0)
{
if(n & 0x01)
{
buffer[--j] = '1';
} else
{
buffer[--j] = '0';
}
n >>= 1;
}
printf("%s ",buffer);
Why do I get the thread error on the NSLog(#"%#", numbers[i]); line?
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSMutableArray *numbers = [NSMutableArray array];
int i;
//Create an arry with the number 0-9
for (i = 0; i < 10; ++i) {
numbers[i] = #(i);
//Sequence through the array and display the values
for (i = 0; i < 10; ++i) {
NSLog(#"%#", numbers[i]);
//Look how NSLog can display it with a singe %# format
NSLog(#"====== Using a single NSLog");
NSLog(#"%#", numbers);
}
}
}
return 0;
}
You're receiving an exception because you have your two for loops nested, and the inner one is trying to iterate through ten values in numbers array, but trying to do that before you've done populating the array in the outer loop.
I presume you do not want those for loops nested:
NSMutableArray *numbers = [NSMutableArray array];
int i;
//Create an array with the number 0-9
for (i = 0; i < 10; ++i)
numbers[i] = #(i);
//Sequence through the array and display the values
for (i = 0; i < 10; ++i)
NSLog(#"%#", numbers[i]);
//Look how NSLog can display it with a single %# format
NSLog(#"====== Using a single NSLog");
NSLog(#"%#", numbers);
NSString *strVal= #"BAAA";
How do I convert the above string into a bit value? Should I do the byte conversion before this?
Need help on this. I have previously checked this question for integer conversion.
Whether you convert the string to UTF-8 first is up to you; it depends what you want. Internally NSString stores characters as UTF-16 (might be UCS-2 actually) using the unichar type, so you need to decide whether you want the binary of the internal representation or of some other external encoding.
I expect you want the binary of the UTF-8 encoding, so try this (tested):
#import <Foundation/Foundation.h>
#interface BinFuncs : NSObject
+ (NSString *)binaryOfString:(NSString *)str;
#end
#implementation BinFuncs
+ (NSString *)binaryOfString:(NSString *)str {
NSMutableString *binStr = [[NSMutableString alloc] init];
const char *cstr = [str UTF8String];
size_t len = strlen(cstr);
for (size_t i = 0; i < len; i++) {
uint8_t c = cstr[i];
for (int j = 0; j < 8; j++) {
[binStr appendString:((c & 0x80) ? #"1" : #"0")];
c <<= 1;
}
}
return binStr;
}
#end
int main(int argc, const char **argv) {
#autoreleasepool {
for (int i = 1; i < argc; i++) {
NSString *binStr = [BinFuncs binaryOfString:[NSString stringWithUTF8String:argv[i]]];
NSLog(#"%#", binStr);
}
}
return 0;
}
$ clang -o binstr binstr.m -framework Foundation
$ ./binstr 'the quick brown fox jumps over the lazy dog'
2013-09-30 09:19:49.674 binstr[58474:707] 01110100011010000110010100100000011100010111010101101001011000110110101100100000011000100111001001101111011101110110111000100000011001100110111101111000001000000110101001110101011011010111000001110011001000000110111101110110011001010111001000100000011101000110100001100101001000000110110001100001011110100111100100100000011001000110111101100111
uint8_t *var;
var=//something;
Now I want to loop through each element of this var
how to do this
please help
Make loop like in plain C. uint8_t *var is just an C array.
for (uint8_t i = 0; i < ARRAY_LENGTH; ++i) {
var[i] = ...; // Do whatever you want
}
For example
uint8_t *v = (uint8_t *)malloc(5 * sizeof(uint8_t));
for (uint8_t i = 0; i < 5; ++i) {
v[i] = i;
}
for (uint8_t i = 0; i < 5; ++i) {
NSLog(#"%d\n", v[i]);
}
free(v);
Note that uint8_t is the same as unsigned char:
#ifndef _UINT8_T
#define _UINT8_T
typedef unsigned char uint8_t;
#endif /*_UINT8_T */
I need to convert an NSarray filled with NSStrings and return this c array to the function.
-(char**) getArray{
int count = [a_array count];
char** array = calloc(count, sizeof(char*));
for(int i = 0; i < count; i++)
{
array[i] = [[a_array objectAtIndex:i] UTF8String];
}
return array;
}
I have this code, but when should i free the memory if I'm returning stuff?
You need to allocate memory for each string in the array as well. strdup() would work for this. You also need to add a NULL to the end of the array, so you know where it ends:
- (char**)getArray
{
unsigned count = [a_array count];
char **array = (char **)malloc((count + 1) * sizeof(char*));
for (unsigned i = 0; i < count; i++)
{
array[i] = strdup([[a_array objectAtIndex:i] UTF8String]);
}
array[count] = NULL;
return array;
}
To free the array, you can use:
- (void)freeArray:(char **)array
{
if (array != NULL)
{
for (unsigned index = 0; array[index] != NULL; index++)
{
free(array[index]);
}
free(array);
}
}
array you return will be caught in some char** identifier in calling environment of getArray() function using that you can free the memory which you have allocated using calloc() inside getArray() function
int main()
{
char **a=getArray();
//use a as your requirement
free(a);
}