Related
I want to change it with as little code as possible.
The code below can change the color, but these are long-winded.
import { createTheme } from "#mui/material/styles";
const shadowKeyUmbraOpacity = 0.05;
const shadowKeyPenumbraOpacity = 0.035;
const shadowAmbientShadowOpacity = 0.03;
function createShadow(...px: number[]) {
return [
`${px[0]}px ${px[1]}px ${px[2]}px ${px[3]}px rgba(0,0,0,${shadowKeyUmbraOpacity})`,
`${px[4]}px ${px[5]}px ${px[6]}px ${px[7]}px rgba(0,0,0,${shadowKeyPenumbraOpacity})`,
`${px[8]}px ${px[9]}px ${px[10]}px ${px[11]}px rgba(0,0,0,${shadowAmbientShadowOpacity})`,
].join(',');
}
export const theme = createTheme({
// Values from https://github.com/material-components/material-components-web/blob/be8747f94574669cb5e7add1a7c54fa41a89cec7/packages/mdc-elevation/_variables.scss
shadows: [
'none',
createShadow(0, 2, 1, -1, 0, 1, 1, 0, 0, 1, 3, 0),
createShadow(0, 3, 1, -2, 0, 2, 2, 0, 0, 1, 5, 0),
createShadow(0, 3, 3, -2, 0, 3, 4, 0, 0, 1, 8, 0),
createShadow(0, 2, 4, -1, 0, 4, 5, 0, 0, 1, 10, 0),
createShadow(0, 3, 5, -1, 0, 5, 8, 0, 0, 1, 14, 0),
createShadow(0, 3, 5, -1, 0, 6, 10, 0, 0, 1, 18, 0),
createShadow(0, 4, 5, -2, 0, 7, 10, 1, 0, 2, 16, 1),
createShadow(0, 5, 5, -3, 0, 8, 10, 1, 0, 3, 14, 2),
createShadow(0, 5, 6, -3, 0, 9, 12, 1, 0, 3, 16, 2),
createShadow(0, 6, 6, -3, 0, 10, 14, 1, 0, 4, 18, 3),
createShadow(0, 6, 7, -4, 0, 11, 15, 1, 0, 4, 20, 3),
createShadow(0, 7, 8, -4, 0, 12, 17, 2, 0, 5, 22, 4),
createShadow(0, 7, 8, -4, 0, 13, 19, 2, 0, 5, 24, 4),
createShadow(0, 7, 9, -4, 0, 14, 21, 2, 0, 5, 26, 4),
createShadow(0, 8, 9, -5, 0, 15, 22, 2, 0, 6, 28, 5),
createShadow(0, 8, 10, -5, 0, 16, 24, 2, 0, 6, 30, 5),
createShadow(0, 8, 11, -5, 0, 17, 26, 2, 0, 6, 32, 5),
createShadow(0, 9, 11, -5, 0, 18, 28, 2, 0, 7, 34, 6),
createShadow(0, 9, 12, -6, 0, 19, 29, 2, 0, 7, 36, 6),
createShadow(0, 10, 13, -6, 0, 20, 31, 3, 0, 8, 38, 7),
createShadow(0, 10, 13, -6, 0, 21, 33, 3, 0, 8, 40, 7),
createShadow(0, 10, 14, -6, 0, 22, 35, 3, 0, 8, 42, 7),
createShadow(0, 11, 14, -7, 0, 23, 36, 3, 0, 9, 44, 8),
createShadow(0, 11, 15, -7, 0, 24, 38, 3, 0, 9, 46, 8),
],
});
createShadow function and others referred to the link below.
https://github.com/mui/material-ui/blob/master/packages/mui-material/src/styles/shadows.js
So, I want to know how to change it more wisely.
I am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}
I am writing code where five random numbers are given a value. Those five values are then sorted in an array in numerical order. However, when one value is equaled to 1, I want that number to be in the middle of the sorted array.
When there is no number 1, the sorted array will work correctly.
let fourthRandomNumber:Int = Int(arc4random_uniform(52))
let fifthRandomNumber:Int = Int(arc4random_uniform(52))
let sixthRandomNumber:Int = Int(arc4random_uniform(52))
let seventhRandomNumber:Int = Int(arc4random_uniform(52))
let eighthRandomNumber:Int = Int(arc4random_uniform(52))
var loFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var hiFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
let loFourthRandomNumber:Int = loFourthRandomNumberOutput [fourthRandomNumber]
let loFifthRandomNumber:Int = loFifthRandomNumberOutput [fifthRandomNumber]
let loSixthRandomNumber:Int = loSixthRandomNumberOutput [sixthRandomNumber]
let loSeventhRandomNumber:Int = loSeventhRandomNumberOutput [seventhRandomNumber]
let loEighthRandomNumber:Int = loEighthRandomNumberOutput [eighthRandomNumber]
let hiFourthRandomNumber:Int = hiFourthRandomNumberOutput [fourthRandomNumber]
let hiFifthRandomNumber:Int = hiFifthRandomNumberOutput [fifthRandomNumber]
let hiSixthRandomNumber:Int = hiSixthRandomNumberOutput [sixthRandomNumber]
let hiSeventhRandomNumber:Int = hiSeventhRandomNumberOutput [seventhRandomNumber]
let hiEighthRandomNumber:Int = hiEighthRandomNumberOutput [eighthRandomNumber]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var loDealerCardsArray = [loFourthRandomNumberOutput [fourthRandomNumber], loFifthRandomNumberOutput [fifthRandomNumber], loSixthRandomNumberOutput [sixthRandomNumber], loSeventhRandomNumberOutput [seventhRandomNumber], loEighthRandomNumberOutput [eighthRandomNumber]]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var hiDealerCardsArray = [hiFourthRandomNumberOutput [fourthRandomNumber], hiFifthRandomNumberOutput [fifthRandomNumber], hiSixthRandomNumberOutput [sixthRandomNumber], hiSeventhRandomNumberOutput [seventhRandomNumber], hiEighthRandomNumberOutput [eighthRandomNumber]]
loDealerCardsArray.sort()
loDealerCardsArray[0]
loDealerCardsArray[1]
loDealerCardsArray[2]
print (loDealerCardsArray[0] + loDealerCardsArray[1] + loDealerCardsArray[2])
//NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
hiDealerCardsArray.sort()
hiDealerCardsArray[2]
hiDealerCardsArray[3]
hiDealerCardsArray[4]
print (hiDealerCardsArray[2] + hiDealerCardsArray[3] + hiDealerCardsArray[4])
////NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
var DealerCardsArray = [fourthRandomNumber, fifthRandomNumber, sixthRandomNumber, seventhRandomNumber, eighthRandomNumber]
DealerCardsArray.sort()
DealerCardsArray[0]
DealerCardsArray[1]
DealerCardsArray[2]
DealerCardsArray[3]
DealerCardsArray[4]
For example, five cards have a value of [0, 1, 2, 9, 10], and the array is sorted correctly. I am trying to sort the array so that the 1 is ALWAYS in the middle.
When an ace (value of 1 or 11) appears in the sorted array I want it to look as follows: [0, 2, 1, 9, 10]
Thank you in advance for your assistance
Something like:
var cards = [0, 1, 2, 9, 10]
// Get a list of all the aces in the array.
let aces = cards.filter { $0 == 1 || $0 == 11 }
// Now take the aces out of the array.
cards = cards.filter { $0 != 1 && $0 != 11 }
// Sort the array (unnecessary if its definitely already sorted).
cards = cards.sort()
// Re-insert the aces into the middle.
aces.forEach { cards.insert($0, atIndex: cards.count/2) }
// [0, 2, 1, 9, 10]
Might do the trick (haha)!
var dealerCardsArray = [0, 1, 2, 9, 10]
dealerCardsArray.sort()
// Only if the value 1 exists in the array, remove it and add it back in the center position
if let indexOfOne = dealerCardsArray.index(of: 1) {
dealerCardsArray.remove(at: indexOfOne)
dealerCardsArray.insert(1, at: array.count/2)
}
print(dealerCardsArray) // [0,2,1,9,10]
Alright so I am new here so please be easy on me :)
I am currently developing on a uC in C and using linked lists to create structures every time the user wants to create one. Example:
typedef struct {
char* dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
}listelement;
listelement * AddItem (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[L][Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
// move down to the end of the list
while (listpointer -> link != NULL)
listpointer = listpointer -> link;
listPointerTemp = listpointer;
listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
// on fail end links becomes NULL already above
if(listpointer -> link != NULL){
listpointer = listpointer -> link;
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return lp;
}
else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
if(listpointer != NULL){
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc (sizeof(name));
if(listpointer -> dataitem != NULL){
for(int16_t i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return listpointer;
}
}
So now I want to add structures already stored in memory using const as such:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
const char dataitem1[4] = {'T','r','o','n'};
const int16_t wordSize1 = 10, nameSize = 4;
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement Tron = { dataitem1, NULL, wordSize1, libWord1, 0x00 };
Alright so even if you skip reading all of that, I was just wondering what a good way of somehow create constant structs (do I have to create a new structure format just for const types?) and then link the beginning of the RAM linked lists when user creates first one to the Flash portion of linked lists created on startup?
Let me know if more information is needed. I tried not to put too much code on here.
Thanks!
So here is the change and the effects:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// ON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord2[10][Q] = { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement* Tron = { "Tron", NULL, 10, libWord1, 0x00 };
// next element links to Tron . . .
const listelement* On = { "On", Tron, 10, libWord2, 0x01 };
// next element links to On . . . and so on . . .
listelement* constLinkInit(void){
return On;
}
I know I didn't do your global array of structs but ignoring that and passing the last struct to this guy in a nother file using a function.
listelement *listpointer;
// initially listpointer is initialized to NULL but now set to end of link list of FLASH
listpointer = constLinkInit();
And the going to print links like I had been doing for user defined structs dynamically (which had worked) fails when pointing to the next link even though the address passed to listpointer (On) is correct.
void PrintQueue (listelement * listpointer) {
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
if (listpointer == NULL){
usart_write_line(&AVR32_USART0,"queue is empty!\r\n");
}else{
//usart_write_line(&AVR32_USART0," Word List \r\n");
//usart_write_line(&AVR32_USART0,"--------------------------------------------\r\n");
while (listpointer != NULL) {
//usart_write_line(&AVR32_USART0, listpointer -> dataitem);
//usart_write_line(&AVR32_USART0,"\r\n");
//writeCOM_int_array(listpointer -> libWord , listpointer -> wordSize);
listpointer = listpointer -> link; // <---- FAILS HERE
}
}
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
When pointing to the next link it goes to the wrong address . . .
Thanks for the help!
Something like this should work:
#include <stdlib.h>
#include <stdint.h>
#define Q 5
typedef struct {
char *dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
} listelement;
int16_t zzz[Q] = { 11, 22,33,44,55};
listelement global_array[] =
{{ "Sron", global_array+1, 2, zzz , 0 }
,{ "Tron", global_array+2, 2, zzz , 0 }
,{ "Uron", global_array+3, 2, zzz , 0 }
,{ "Vron", NULL , 2, zzz , 0 }
};
This may sound somewhat stupid, but I have to know as I'm writing a bingo board in C.
#include <stdio.h>
typedef struct {
int a;
int b;
int c;
int d;
int e;
} row;
typedef struct {
row one;
row two;
row three;
row four;
row five;
} bingo_board;
void initialize_columns()
{
bingo_board board = {
.one = {1, 2, 3, 4, 5},
.two = {6, 7, 8, 9, 10},
.three = {11, 12, 13, 14, 15},
.four = {16, 17, 18, 19, 20},
.five = {21, 22, 23, 24, 25}
};
}
Is this possible?
It can be done simply as
void initialize_columns()
{
bingo_board board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
}
Or even as
void initialize_columns()
{
bingo_board board = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25
};
}
No need to "tag" every row. The tagged syntax is available in C99 though, and what you have in your example is already correct for C99.
Because structs are first class citizens in c, assignment is well defined, this lets you
static bingo_board initial_board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
void init_board(bingo_board *b)
{
*b = initial_board;
}
Which seems to be what you want. If you do declare the board within the function, I would suggest declaring it static, because you don't modify it, so persistent changes are ok, and so that the function doesn't have to grow the stack as much on every call.