How to print each element from array of unknown length in C - c

.I have a big array of coordinates that looks like this
triangle_t teapot_model[] = {
{
.x1=5,
.y1=10,
},
{
.x1=20,
.y1=30,
},
(keeps going)
How can I print all of the items in this array without knowing their position?
I want this output:
Output:
.x1=5 y1=10
.x1=20 .y1=30

Array in C always has a size although implicit in your case.
To simply print each element of your array, using the below code should suffice
int sizearray = sizeof teapot_model / sizeof *teapot_model;
for (int i = 0; i < sizearray; i++)
{
printf(".x1=%d .y1=%d\n", teapot_model[i].x1, teapot_model[i].y1);
}

Related

How do I check if there is an element in a Matrix?

I'm Trying to check in my matrix of dimension [10][10], which spots are available to store data (String) there and which are occupied.
The code basically goes through the whole matrix and checks every spot.
I have tried using the strlen and != NULL but everything just prints that the spot is free.
char parque[10][10];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
parque[i][j] = "";
}
}
parque[5][5]="f47ac10b-58cb-4372-a567-0e02b2c3d499,ANR";
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(parque[i][j] != "") {
printf("The Spot [%d][%d] is taken",i,j);
} else {
printf("The Spot [%d][%d] is free",i,j);
}
}
}
Basically the spot [5][5] should print that it's taken, at least that's what I want it to do...
Thanks in advance!
Your declaration
char parque[10][10];
declares a two-dimensional array of char. If you compile your code with a strict compiler, you'll get an error:
error: assignment makes integer from pointer without a cast [-Wint-conversion]
parque[i][j] = "";
^
What you did mean is to make an array of pointers to const char, like here:
const char* parque[10][10];
Then your program will say that The Spot [5][5] is taken.
You can't use !=. You need to use strcmp. And, of course, you need to initialize your array content before iterating it and using its values to compare with "" string.
This condition:
if(parque[i][j] != "")
Will become:
if (strcmp(parque[i][j], ""))

2D array adding extra character

This is my code for appending to the 2D array and displaying it line by line into a grid.
void map_print() {
char map[head->xdim][head->ydim]; // Generate 2D array for map
memset( map, 0, sizeof(map));
FEATURE *temp=head->next; // Get the pointer of the head of linked list
while(temp!=NULL) // Generated the map with the features
{
for (int xdim = 0; xdim < temp->xdim; xdim++){
map[temp->yloc][temp->xloc + xdim] = temp->type;
printf("X axis: Appeding to map[%d][%d]\n",temp->yloc,temp->xloc+xdim);
}
for (int ydim = 0; ydim < temp->ydim; ydim++){
map[temp->yloc + ydim][temp->xloc] = temp->type;
printf("Y axis: Appeding to map[%d][%d]\n",temp->yloc + ydim,temp->xloc);
}
temp=temp->next;
}
for (int i = 0; i < head->ydim; i++) { // Print out the map
for (int j = 0; j < head->xdim; j++) {
//printf("%c ", map[i][j]);
printf("map[%d][%d](%c)",i,j,map[i][j]);
}
printf("\n");
}
}
Based on the printf, it should only append to the following coordinates. However, map(1)(4),map(2)(4),map(3)(4),map(4)(4) are printing * which i had not appended to it.
I cant find any line of my code that is adding that extra character
You mixed x and y. The declaration is char map[head->xdim][head->ydim]; ([x][y]), but you are using it like map[temp->yloc][temp->xloc + xdim] = temp->type; ([y][x]).
If your array's size is [10][5] and you are accessing [0][9] it would invoke undefined behaviour (because of out of bounds access) and one possibility is that it would access [1][4] (the tenth element in the 2D array) instead.

c,Finding length of pointer array

I am trying to find length of pointer array.
Here is my code:
char **histargv[histsize];
histargv[hist] = (char**)malloc(sizeof(char *)*arg);
variable arg is not constant. Each index of histargv array has different arg value.
So how can I find arg value for each index?
Thanks
If the pointers are for strings, then you can figure out the length of the strings (not the size of the memory block) by using strlen. I.e. with:
for (size_t i = 0; i < 100; ++i) {
histargv[i] = read_some_string_somehow_and_return_in_new_buffer();
}
you can print the string including their lengths like this:
for (size_t i = 0; i < 100; ++i) {
printf("%3zu %zu %s\n", i, strlen(histargv[i]), histargv[i]);
}
If this are not strings, then you should follow the link in Eli Sadoffs comment.

How to find the length of an integer array passed as an argument in c?

I need to calculate the length of passed array in monkey method. How to do so, as input decays to a pointer inside monkey method...?
int monkey(int input1[])
{
//Write code here
}
Constraints:
1) You are not allowed to change the function signature.
int monkey(int input1[], size_t arraySize)
{
}
Passing the size of the array is the most usual method.
Alternatively you can do something like C strings, and add a sentinel value to the end of your array (max value, 0, -1, etc) and count the number of elements before this sentinel value.
There are no other alternatives I can think of.
In the general case, you don't. As it's just a pointer, information about the array is not available.
So you should pass in a size, or wrap it in a struct that contains the size.
Or as it's an array of int, you could use the first value in the array as the size.
Alternatively, depending on your use-case, you could zero-terminate (or else unambiguously mark the end of) the data in the array.
There is no way to do such thing in C. The definition of your function
int monkey(int input[])
is formally equivalent to
int monkey(int *input)
so you ALWAYS need to pass the length of your array as an additional parameter
Two options:
int array[10+1];
array[0] = 10;
// fill in the rest in array[1...10]
monkey(array);
// monkey() checks input1[0] to see how many data elements there are
and
int array[10+1];
array[0] = 10;
// fill in the rest in array[1...10]
monkey(array + 1);
// monkey() checks input1[-1] to see how many data elements there are
Alternatively, you can pass the size/count via a different channel, for example, a global variable, which you may guard with a critical section or a semaphore/lock if necessary.
When you pass the array to the function, you can store the length as the first element of the array:
int monkey(int input1[])
{
int len = input1[0];
for (int i = 1; i < len; ++i) {
// do something with the array
}
}
//...
int array[20];
array[0] = 20;
monkey(array);
An alternative method is to do it using pointer arithmetics instead, so that the function can treat the array as starting from 0:
int monkey(int input1[])
{
int len = *(input1 - 1);
for (int i = 0; i < len; ++i) {
// do something with the array
}
}
//...
int array[20];
array[0] = 19;
monkey(array + 1);
But it's the same thing really. The only advantage this has is guarding against mistakes like starting from 0 instead of 1. But then this means that you could forget to pass array + 1 when you call it. So meh.
int main()
{
int arr[] = { 62,34,4,4,4,3,2,-1 };
monkey(arr);
return 0;
}
int monkey(int input1[])
{
int n = 0;
while (a[n] != -1)
{
n++;
}
printf_s("Length of input1[] is: %d", n);
return n;
}
int monkey(int input1[])
{
//Write code here
}
int len = sizeof(input1)/sizeof(input1[0]);
OR
int len = sizeof(input1)/sizeof(int);
is WRONG..
IT will always give 2..as input1 is a LONG POINTER AND so size is 8,,and sizeof(int) is 4..
if you want to find the length there is 2 ways...
1) pass the length...
( you cannot i suppose )
2) if you have idea..about the input1 array...say ( TECH GIG competition )
try to find out the number contained in the undefined parts of the array..
say here i found out for the sample ..{1,2,3,4,5} => input1[6] => contains the number -9999
so just wrote a while loop to find the length
while(input1[len]!=-9999)
len++;
to find out that
input1[6] => contains the number -9999
i use in the answer
return input1[6]..to check
that -9999 was the answer
i just submitted the answer for TECH GIG competition

Scan an array using strcmp

I'm wondering if there's a way you can scan an array for a match using strcmp. I know the parameters passed to strcmp are C strings. So something like this wouldn't work:
strcmp(arrayofstrings[x], c-string)
It would work as long as the arguments can be reduced to of type const char*.
char *a[] = { "Hello", "Hello" }; // Array of pointers to c strings
if ( !strcmp(a[0],a[1]) ){
// true in this case
}
If you're trying to search the entire array, rather than just comparing two elements, you will need a loop.
const int N = 10;
const char * desired = "desiredString";
char * arrayOfStrings[N];
// You should initialize the elements
// in arrayOfStrings[] before searching
// Searching an unsorted array is O(N)
for(i = 0; i < N; i++)
{
if(strcmp(arrayOfStrings[i], desired) == 0)
{
printf("Found %s.", desired);
break;
}
}

Resources