Fill char *array from 0 to x [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have a problem with a char *array in c (gcc linux)
on debian it works, but on another systems (yocto,raspbian) come a segmentation fault
The working Code in Debian:
char *myarray;
for (i=0;i<999;i++){
printf(myarray, "%i", i);
//do something with string to compare in file
}
But this Code fault on another Systems, i have tried to make a Array:
char *myarray[999]={"0","1","2"};
for (i=0;i<999;i++){
//do something with string[i] to compare in file
}
This Code also works but i dont like to fill a array from hand to "999"
I haven't found a method to make a char *string[arr] from "0"-"999" in a loop

Well , you can use sprintf-
char *array[1000];
for(int i=0;i<1000;i++){
array[i]=malloc(10*sizeof(**array)); //allocate memory to pointer
if(array[i]!=NULL){ //check return of malloc
sprintf(array[i],"%d",i);
}
}
Note- Just remember to free the allocated memory.

It's very unclear what you're after.
If you want to build an array holding the strings "0" through "999", you can do it using snprintf():
char array[1000][4]; /* Wastes some space, but not a great deal. */
for(int i = 0; i < 1000; ++i)
snprintf(array[i], sizeof array[i], "%d", i);
then you can print e.g. 452 like so:
printf("452 is %s\n", array[452]);

Related

Size of char Array doesn't change(C) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Somehow my program outputs the same size no matter how long the array gets, do you know what i did wrong?
char charArray[] = "STRING";
int size = sizeof(charArray) / 2 - 1;
printf("%d", size);
Output: 3
(i have to create a program which finds a string in another string thats why i am substracting 1 at the end to find the length of the word i want to find)
If you just want to get the length of your string, you could use strlen from the string library, of implement your own one:
size_t my_strlen(const char *str)
{
size_t i = 0;
while (str[i] != '\0')
i++;
return (i);
}
with this function, my_strlen("STRING") will return 6.

sizeof vs strlen in long running programs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Which of these is faster and can be used in programs which runs 100s of times in a loop (for size of strings)?
sizeof - it is a macro and a compile time expression.
or
strlen - it's run time expression.
In my mind I want to avoid strlen as it's a function and calling it again and again might slow things down - Am I correct?
These do not do the same thing.
sizeof is an operator which, in most cases, is evaluated at compile time. It gives the size in bytes of a variable, including arrays and structs. In contrast, strlen is a function which returns the length of the string passed to it.
For example:
char str[100] = "hello";
printf("size = %zu\n", sizeof str); // prints 100
printf("len = %zu\n", strlen(str)); // prints 5
That being said, if you're looking to optimize something like this:
int i;
for (i=0; i<strlen(str); i++) {
...
}
You should do this instead:
int i;
int len = strlen(str);
for (i=0; i<len; i++) {
...
}
If your strings are not string literals or arrays then strlen is your only choice. Either way, it will be fast—strlen is usually very heavily optimized.

Allocating 10kb of memory and printing addresses and content [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to create a program which should allocate 10kb of memory and prints the memory addresses at groups of 4 bytes and its content. Here is what i want the output to be like:
0XAABBCCEB CDCDCDCD
0XAABBCCD8 FFA0B0C0
0XAABBCCD4 00FF00FF
0XAABBCCD0 00000000
I don't really know how to get to that output. I know that i need to use malloc and i know that i need to use the right operator to print it in hexadecimal form, but i don't know how to print the content and how to allocate exactly 10kb
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define _10KB 10240
int main(){
int i;
uint32_t *arr;
if((arr = malloc(_10KB)) == NULL){
perror("malloc failed");
}
for(i = 0; i < (_10KB/4); i++){
printf("0x%p %.8x\n", &arr[i], arr[i]);
}
free(arr);
return 0;
}

Dynamic Array of strings [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to create an array of strings but I keep getting an error.
Can you help me figure out what's wrong with this code?
int size;
scanf("%d",&size);
char** arr;
arr=(char**)malloc(sizeof(char*)*size);
You can simply use array of n number of pointers to char. Then use a loop to allocate space for those.
int n, size;
scanf("%d %d", &n, &size);
char *arr[n];
for( int i = 0; i < n; ++i ){
arr[i] = malloc( size * sizeof(char) );
}

Printing one dimensional array in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In Python I can define a list, and print it all with one command:
lst = [1,2,3]
print lst
[1,2,3]
Is there any equivalent command in C ? (without using loops), or do I have to print every element by himself?
Thanks!
You can use recursion like in this pseudo code:
void print_array(item array[], size_t len)
{
if (len == 0)
return;
print_item(*array);
print_array(array + 1, len - 1);
}
Modern C compilers can optimize away tail-recursion, so this is likely not much less efficient than a loop.
If it's a string then yes, you can dump the entire contents of the array as long as it's \0 (nul) terminated...fprintf and the likes dump entire arrays all of the time.
char *p;
char string[12] = "A string";
for (p = &string[0]; *p != '\0'; p++)
fprintf(stdout, "%c", *p);
Is akin to
printf("%s", string);

Resources