I have initialized the array for one of the element in the structure, there were some errors in printing the output, please point the errors and guide in solving it . Thanks!
#include<stdio.h>
typedef struct person
{
int row[3];
int age;
}PERSON;
int main()
{
int i;
PERSON p;
PERSON *pptr=&p;
pptr->row[3] = {4,5,6};
for (i = 0; i < 3; i++) {
printf (" %d\n", pptr->row[i]);
}
return 0;
}
Array objects in C language are not assignable. You cannot set the values in the entire array by using assignment. So, to do what you are trying to do using assignment syntax is impossible.
You had a chance to initialize your array at the point of declaration, i.e. when you defined object p
PERSON p = { { 4, 5, 6 } };
but you did not use that chance. After that it is too late to do it using pure core language features.
To set the values in the entire array after the point of declaration you can use a library function, like memcpy, in combination with a compound literal
memcpy(pptr->row, (int []) {4, 5, 6}, sizeof pptr->row);
You can't assign values to array like this: pptr->row[3] = {4,5,6};. Such syntax is valid only at initialization. You need to set each value manually or initialize your array with the values you want, something like this: PERSON p = {{4,5,6}, 0};.
#include<stdio.h>
typedef struct person
{
int row[3];
int age;
}PERSON;
int main()
{
int i;
PERSON p;
PERSON *pptr=&p;
pptr->row[0] = 4;
pptr->row[1] = 5;
pptr->row[2] = 6;
for (i = 0; i < 3; i++) {
printf (" %d\n", pptr->row[i]);
}
return 0;
}
You can't initialize array like this pptr->row[3] = {4,5,6};, You can use the above method or use for loop to initialize array..
When you have done PERSON p;. object p is created containing indeterminate values.
It means all the data members are already initialized with garbage since it's on stack.
So, pptr->row[3] = {4,5,6}; is not the initialization of the array an not allowed in this case. The for loop method is the best in this case.
for (i = 0; i < 3; i++)
scanf("%d",&(pptr->row[i])); // prenthrsis is for human readability
Related
#include "stdio.h"
int printsomething(int *array, int arrayreturn[5]) {
int i;
for(i = 0; i < 10; ++i) {
printf("%d\n", array[i]);
}
for(i = 0; i < 5; ++i) {
int &arrayreturn[i] = {i};
}
return 0;
}
int main() {
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// int *arraypointer = &array;
int arrayp[5];
int i;
printsomething(array, arrayp);
for(i = 0; i < 5; ++i) {
printf("%d\n", arrayp[i]);
}
return 0;
}
I am learning C and right now just playing with arrays and pointers trying to get comfortable. This bit of code has the goal of passing an array to a function, which was successful before I added the second part. That second part being assigning values in the called function to an already initialized array. Since we can't directly return an array I understood this was the way to do it. What exactly do you all think is going wrong here? And I just completely off the target?
If you want to assign values to the array elements you need to use [] to access the elements and = to assign them. I cannot really explain your code because it is unclear how you came to the conclusion that you need to write int &arrayreturn[i] = {i};. Your loop can be this:
for(i = 0; i < 5; ++i) {
arrayreturn[i] = i;
}
the first problem is that when you have a parameter of the form int arrayreturn[5] you actually just pass an int pointer not an entire array of 5 elements. int arrayreturn[5] and int *arrayreturn compile to exactly the same cpu instructions. I never use the int arrayreturn[5] syntax because i think it is confusing so i rather just pass a pointer and this is common practice as far as i know.
secondly in the second part of your code you try to declare a new array of size i by calling int &arrayreturn[i] = {i} this is not possible because of multiple reasons mostly because you cant dynamically allocate arrays on the stack. it should be arrayreturn[i] = i
My function arrayReturn() returns the address of array a.
In main() I assign the address to a pointer p and I print the array.
Is there any way to know the size of the array from the function? Assuming we don't know the size,
it is possible, instead of for(i = 0; i < 3; i++) to write something like
for(i = 0; i < sizeof(p); i++)?
#include <stdio.h>
int * arrayReturn();
int main()
{
int *p = arrayReturn();
int i;
for(i = 0; i < 3; i++)
{
printf("%d ", *(p+i));
}
return 0;
}
int * arrayReturn()
{
static int a[] = {11, 22, 33};
return &a;
}
Pointers store the address of a single object - that object may be the first element of an array, it may be an element in the middle of an array, or it may be a single object that isn't part of an array.
There's no way to determine from the pointer value itself whether it points to an element of an array or not.
You'll have to return the array size as a separate item, either as a writable parameter or as a member of a struct type that also stores the pointer.
I got really confused why I can not use while loop to access the entire value for reading all of struct...Thank you guys... I think about 3 hours but have no clue. I do not know why does the value is not realized by the program, since I already defined that this is a struct.
#include <stdio.h>
struct card{
int isRed;
int hasLetter;
union{
int charValue;
int intValue;
}value;
};
typedef struct card typeCard;
int deckValue(typeCard *deck[])
{
int i = 0;
int sum = 0;
while(deck[i] != NULL){
sum += deck[i].value.intValue;
i += 1;
}
return sum;
}
int main()
{
int sum;
typeCard card1 = {.isRed = 1,.hasLetter = 0,.value.intValue = 200};
typeCard card2 = {.isRed = 100,.hasLetter = 0,.value.intValue = 200};
typeCard deck[] = {card1,card2,NULL};
sum = deckValue(deck);
printf("%d",sum);
return 0;
}
The problem is that the parameter to your function:
int deckValue(typeCard *deck[])
Doesn't match how you're using it:
sum += deck[i].value.intValue;
The function definition says that desk is an array of pointers to typeCard. So when you do deck[i].value it thinks that you have an array of typeCard.
You instead want:
sum += deck[i]->value.intValue;
This will dereference the pointer for the array element.
Also in main, you're not passing an array of pointers to typeCard:
typeCard deck[] = {card1,card2,NULL};
sum = deckValue(deck);
You're passing an array of typeCard. Since you want an array of pointers so you can use a NULL pointer as a sentinel, you need to change the definition to an array of pointers, and initialize the array elements accordingly:
typeCard *deck[] = {&card1, &card2, NULL};
If i had an array such as int numbers[5] i could assign values to it with numbers[0] = 1 or numbers[3] = 4. Then if i had a struct such as
struct structName
{
int number0;
int number1;
int number2;
};
is there any way to do something like the following (note this is not working code)
int main(void)
{
struct structName name; //how could i declare this to do the following
for(int i = 0; i < 2; i++)
{
name[i] = i; //maybe name.[i]
}
}
so is there a way to write name[ variable ] = someNumber to assign someNumber to say number0 (if variable was 0) or number2 (if variable was 2). ive been looking for days and cant find anything that does this. (maybe i just don't know what to look for)
is there any way to do something like the following
No, there's no way to access the fields of the structure by index. You use the names of the fields instead:
struct structName name;
name.number0 = someNumber;
name.number1 = someOtherNumber;
If you want to access the values by index, use an array instead, even if it's embedded in the structure:
struct structName
{
int numbers[3];
// other fields here
};
Then you can say:
struct structName name;
for (int i = 0; i <= 2, i++) {
name.numbers[i] = i;
}
You could write a function which uses a switch statement that allows you to access fields by index. Something like:
#include<stdio.h>
struct structName{
int number0;
int number1;
int number2;
};
void assign(struct structName * name, int i, int j){
switch(i){
case 0:
name->number0 = j;
break;
case 1:
name->number1 = j;
break;
case 2:
name->number2 = j;
break;
}
}
int main(void){
int i;
struct structName name;
for(i = 0; i <= 2; i++){
assign(&name,i,i);
}
//test:
printf("%d\n",name.number0);
printf("%d\n",name.number1);
printf("%d\n",name.number2);
return 0;
}
(which prints 0,1,2 as expected).
Needless to say, there isn't much point in doing this (as opposed to just having a field which is an array) unless the struct in question is already defined as part of an API or already part of a code base which isn't easily refactored.
Yes, with some weird and inadvisable memory manipulation. You're much better off using an array.
struct structName
{
int numbers[3];
};
int main(void)
{
struct structName name;
for(int i = 0; i <= 2; i++)
{
name.numbers[i] = i;
}
}
Also note that you had some syntax errors in your for loop and an off-by-one error.
Macros with arguments should work
#define name(x) x
So name(1) would become 1. name(2) would become 2 and so on.
In C, there is no spoon.
struct structName name;
int *idx = &name; // First we need a memory address to the struct
for (int i = 0; i < sizeof(name) / sizeof(*idx); ++i) {
// idx[i] == name.numberX
idx[i] = i;
}
Now, if you check the values of name.number0, name.number1, name.number2 you will see they contain the correct values.
This is not a very good way of doing things with structs, but I felt compelled to answer after the top response claims it is impossible.
Let us say I have the following method prototype:
void mix_audio(int *vocal_data_array, int *instrumental_data_array, int *mixed_audio_array, FOURTH ARGUMENT)
{
}
How would I:
Initialize an array_of_arrays before the above argument so as to pass it as the fourth argument?
In the method, make it so that the first value of my array_of_arrays is the array called vocal_data, that the second value of my array is instrumental_data_array and the third value is mixed_audio_array.
How would I later then loop through all the values of the first array within the array_of_arrays.
I hope I'm not asking too much here. I just thought it would be simple syntax that someone could spit out pretty quickly :)
Thanks!
EDIT 1
Please note that although I've showed by my example an array_of_arrays of length 3 I'm actually looking to create something that could contain a variable length of arrays.
Simple array of arrays and a function showing how to pass it. I just added fake values to the arrays to show that something was passed to the function and that I could print it back out. The size of the array, 3, is just arbitrary and can be changed to whatever sizing you want. Each array can be of a different size (known as a jagged array). It shows your three criteria:
Initialization, Assigning values to each index of arrayOfArrays, The function demonstrates how to extract the data from the array of arrays
#include <stdio.h>
void mix_audio(int *arr[3]);
int main() {
int *arrayOfArrays[3];
int vocal[3] = {1,2,3};
int instrumental[3] = {4,5,6};
int mixed_audio[3] = {7,8,9};
arrayOfArrays[0] = vocal;
arrayOfArrays[1] = instrumental;
arrayOfArrays[2] = mixed_audio;
mix_audio(arrayOfArrays);
return(0);
}
void mix_audio(int *arr[3]) {
int i;
int *vocal = arr[0];
int *instrumental = arr[1];
int *mixed_audio = arr[2];
for (i=0; i<3; i++) {
printf("vocal = %d\n", vocal[i]);
}
for (i=0; i<3; i++) {
printf("instrumental = %d\n", instrumental[i]);
}
for (i=0; i<3; i++) {
printf("mixed_audio = %d\n", mixed_audio[i]);
}
}
From your question it sounds like you actually want a struct containing your arrays, something like:
struct AudioData {
int* vocal_data_array;
unsigned int vocal_data_length;
int* instrumental_data_array;
unsigned int instrumental_data_length;
int* mixed_audio_array;
unsigned int mixed_audio_length;
};
For the array allocation using the example of an array of integers:
int** x = malloc (sizeof (int*) * rows);
if (! x) {
// Error
}
for (int i = 0; i < rows; ++i) {
x[i] = malloc (sizeof (int) * columns);
if (! x[i]) {
// Error
}
}