Printing one dimensional array in C [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 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);

Related

Is it possible to have the intersection of two strings without using a loop 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 last year.
Improve this question
I would like to ask you if it was possible to get the characters common to two strings without having to resort to a loop on the character array. I wonder why this could greatly affect the total cost (asymptotically for n-> infinite) of algorithms such as eg. Charm or Eclat (just think that it would be like adding a new cycle to those already present). Thank you.
Specifically, the algorithm I am referring to is the following. As can be seen from the photo (line 6) it is necessary to obtain the intersection by iterating on the indices i and j, so I suppose it is necessary to iterate. I guess I get an O(m + n) best assuming the insert and search operations use O(1).
If your characters are byte-encoded (US-ASCII, KOI8-R, etc), you can create array, where your char is index, and iterate 1st string, and set "1" here. Thereafter, iterate 2nd string, and print only chars, presents in the array. See the example:
void print_intersection(const unsigned char *s1, const unsigned char *s2) {
unsigned char arr[0x100], c;
bzero(arr, sizeof(arr)); // cleanup
while(c = *s1++)
arr[c] = 1;
while(c = *s2++)
if(arr[c] != 0) {
putchar(c);
arr[c] = 0; // Disable print dups
}
}

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.

Copying 1 variable of 1 type to another variable of another type in C [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 6 years ago.
Improve this question
I had an interview yesterday where they had asked me to write a function which accepts 3 arguments,1 source , 1 destination and other the length and this function should copy the value from source to destination based on the length parameter and the types of source and destination could be different.
Can someone please help me write a generic function ?
Thanks a lot in advance
You mean memcpy (or memmove)? :P
A naive implementation (using bytes):
int my_memcpy(void *dest, const void *src, size_t len)
{
if (dest == NULL || src == NULL || src == dest) return -1;
if (len == 0) return 0;
char *dest_bytes = dest;
const char *src_bytes = src;
for(size_t i = 0; i < len; i++) {
dest_bytes[i] = src_bytes[i];
}
return 0;
}
One can optimise using uint64_t pointers (taking care of the remainder with a char *) and loop unrolling to copy more data each iteration of the for loop.

Fill char *array from 0 to x [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 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]);

Is it possible to make a string such as "G" equal a specific number like 50? [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 6 years ago.
Improve this question
For example if I had the letter "B" in a array and want to count how many there are,could I make "B"=1 so I can easily count the number of b's.I do not think this is typecasting since I am do not want to make "B" itself = int B
You don't need to assign to a string, just use an ordinary variable.
int b_count = 0;
char *string = "This is a B and this is another B";
for (char *p = string; *p != 0; p++) {
if (*p == 'B') {
b_count++;
}
}
printf("There are %d B's in the string\n", b_count);

Resources