Difference between const char[] Variable; and "some chars" - c

I have code like this:
const char menu23[] = "2.3. ";
const char menu24[] = "2.4. ";
const char menu25[] = "2.5. ";
const char menu26[] = "2.6. ";
const char menu27[] = "2.7. ";
const char menu28[] = "2.8. ";
MenuEntry menu[] = // MenuEntry is a struct
{
{menu23, MENU2_OPTIONS, 22, 24, 23, 0, 0, 0}, // 23
{menu24, MENU2_OPTIONS, 23, 25, 2, 0, 0, 0}, // 24
{menu25, MENU2_OPTIONS, 24, 26, 0, 0, 0, 0}, // 25
{menu26, MENU2_OPTIONS, 25, 27, 0, 0, 0, 0}, // 26
{menu27, MENU2_OPTIONS, 26, 28, 0, 0, 0, 0}, // 27
{menu28, MENU2_OPTIONS, 27, 29, 0, 0, 0, 0} // 28
}
Can I replace it with this?
MenuEntry menu[] = // MenuEntry is a struct
{
{"2.3. ", MENU2_OPTIONS, 22, 24, 23, 0, 0, 0}, // 23
{"2.4. ", MENU2_OPTIONS, 23, 25, 2, 0, 0, 0}, // 24
{"2.5. ", MENU2_OPTIONS, 24, 26, 0, 0, 0, 0}, // 25
{"2.6. ", MENU2_OPTIONS, 25, 27, 0, 0, 0, 0}, // 26
{"2.7. ", MENU2_OPTIONS, 26, 28, 0, 0, 0, 0}, // 27
{"2.8. ", MENU2_OPTIONS, 27, 29, 0, 0, 0, 0} // 28
}
Is there any functional difference?

There is a big difference.
In this problem, first member of MenuItem must be char * type. (Otherwise first version of code in this question will fail with compile errors.)
char menu23[] = "2.3. "; This is a char array. String is stored in array's memory. Its lifetime is the same as array. If it's defined in a function, it will be destroyed after function returns. If it's defined as global variable, then there is no functional difference with second version. (But still different in implement perspective.)
{"2.3. ", MENU2_OPTIONS, 22, 24, 23, 0, 0, 0}, String in this line is a literal constant. It will be stored in memory's static area. Its lifetime will be the same as your application.

No differences. The first, though, can be more flexible in case you want to change the name of each menu later on (and in case the constant names are used in multiple locations).

Related

How to subtract arrays of integers

I have four arrays of int:
num_defect = [30, 30, 20, 20, 18, 18, 5, 5]
num_fixes = [1, 0, 3, 2, 1, 2, 2, 2]
num_blocks = [0, 0, 0, 0, 2, 2, 1, 0]
num_ext_defects = [1, 1, 0, 0, 2, 2, 2, 1]
I want to display the number of open defects, which is given by:
num_defects - num_fixes - num_blocks - num_ext_defects
So for the reporting, num_defects should now contain:
[28, 29, 17, 13, 12, 0, 2]
I tried:
num_defect.map { |i| i - num_fixes[i] - num_blocks[i] - num_ext_defects[i] }
but it raises:
nil can't be coerced into Fixnum
Any help greatly appreciated.
With
num_defect.map { |i|
i is the element of the array, not its index. If you want your map to work correctly, you'll need an index as well:
num_defect.map.with_index do |element, index|
element - num_fixes[index] - num_blocks[index] - num_ext_defects[index]
end
Use map! instead of map in order to mutate num_defect.
Or if you'd like a nicer version:
a = [30,30,20,20,18,18,5,5]
b = [ 1, 0, 3, 2, 1, 2,2,2]
c = [ 0, 0, 0, 0, 2, 2,1,0]
d = [ 1, 1, 0, 0, 2, 2,2,1]
a.zip(b,c,d).map { |arr| arr.inject(:-) }
# => [28, 29, 17, 18, 13, 12, 0, 2]
If i understand you correctly you might be looking for an array method called each_index.
num_defect.each_index do |i|
num_defect[i] -= num_fixes[i] + num_blocks[i] + num_ext_defects[i]
end
require 'matrix'
(Vector.elements(num_defect) - Vector.elements(num_fixes) -
Vector.elements(num_blocks) - Vector.elements(num_ext_defects)).to_a
#=> [28, 29, 17, 18, 13, 12, 0, 2]
This uses the methods Vector::elements and Vector#to_a. One could write Vector[*arr] in place of Vector.elements(arr), using Vector::[].
If num_defect is to be mutated, you could write num_defect.replace(<above expression>). If
arr = [num_defect, num_fixes, num_blocks, num_ext_defects]
#=> [[30, 30, 20, 20, 18, 18, 5, 5],
# [ 1, 0, 3, 2, 1, 2, 2, 2],
# [ 0, 0, 0, 0, 2, 2, 1, 0],
# [ 1, 1, 0, 0, 2, 2, 2, 1]]
one could use matrix multiplication:
(Matrix.row_vector([1, *[-1]*(arr.size-1)]) * Matrix.rows(arr)).to_a.first
#=> [28, 29, 17, 18, 13, 12, 0, 2]
where
[1, *[-1]*(arr.size-1)]
#=> [1, -1, -1, -1]
This would be convenient and relatively computationally-efficient if arr had a larger number of elements than it does in the example.
This uses the Matrix methods Matrix::row_vector, Matrix::rows and Matrix#to_a. One could write Matrix[*arr] in place of Matrix.rows(arr), using Matrix::[]. One advantage of using rows, however, is that one can add the argument false (Matrix.rows(arr, false)) to avoid copying the elements of arr in the creation of the Matrix object.
[num_defect, num_fixes, num_blocks, num_ext_defects]
.transpose
.map{|first, *rest| first - rest.sum}
# => [28, 29, 17, 18, 13, 12, 0, 2]
Using Enumerator#each_with_object:
num_defect.each_with_index.with_object([]){ |(e, i), a| a << (e - num_fixes[i] - num_blocks[i] - num_ext_defects[i]) }
#=> [28, 29, 17, 18, 13, 12, 0, 2]

Swift using eratosthenes method to get prime numbers

I want to use eratosthenes method
to get prime numbers with swift. I create first function to return new array without those numbers that can be divided for specific multiplier, then create second function to create new array every time with new P multiplier. I wonder why its not work (look like it somehow pass old array, i dont know why). It should print new array of prime numbers at the end:
var simpleArr : [Int] = []
for i in 2...100 {
simpleArr.append(i)
}
func arrayEcludingDivingByP (p: Int, arrToCheck : [Int]) -> Array<Int>{
var tmp : [Int] = []
for (ob, index) in arrToCheck.enumerated() {
var isDividible : Bool = ob % p == 0 ? true : false
if (!isDividible){
tmp.append(ob)
}
}
return tmp
}
var p : Int = 2
func getSimpleNumbersArrayFromArray (p : Int, arrPassed : [Int]) -> Array <Int>{
var tmp : [Int] = []
var newArr = arrayEcludingDivingByP(p: p, arrToCheck: arrPassed)
if (newArr.isEmpty){
// No more p availible, just return tmp
} else {
let newP = p + 1
getSimpleNumbersArrayFromArray(p: newP, arrPassed: newArr)
tmp = newArr
print("tmp array? \(tmp)")
}
return tmp
}
getSimpleNumbersArrayFromArray(p: p, arrPassed: simpleArr)
In console it prints:
tmp array? [1]
tmp array? [1, 2]
tmp array? [1, 2, 3]
tmp array? [1, 2, 3, 4]
tmp array? [1, 2, 3, 4, 5]
tmp array? [1, 2, 3, 4, 5, 6]
tmp array? [1, 2, 3, 4, 5, 6, 7]
tmp array? [1, 2, 3, 4, 5, 6, 7, 8]
tmp array? [1, 2, 3, 4, 5, 6, 7, 9, 10, 11]
tmp array? [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]
tmp array? [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17]
tmp array? [1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23]
tmp array? [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 31]
tmp array? [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47]
tmp array? [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97]
But it suppose to print final iteration with tmp filled with prime numbers. What did i wrong?
You are using newArr to represent the values in the sieve that remain. Your tmp array should be the primes found so far. Also you are using a recursive call which is confusing. I suggest simply grabbing the first value out of newArr which is a prime, add that prime to your list of primes, and then call arrayEcludingDividingByP to filter out the non-primes divisible by that prime. Repeat until newArr isEmpty which will happen when the conditional binding statement while let newP = newArr.first fails and the loop ends:
func getPrimes(arrPassed: [Int]) -> [Int] {
var primes: [Int] = []
var newArr = arrPassed
while let newP = newArr.first {
primes.append(newP)
newArr = arrayEcludingDivingByP(p: newP, arrToCheck: newArr)
}
return primes
}
print(getPrimes(arrPassed: Array(2...100)))
This can be further optimized because once newP * newP is greater than the largest number in newArr (newArr.last!) you are done and you can simply append newArr to primes and set newArr to [].
Also, arrayEcludingDivingByP(p: newP, arrToCheck: newArr) can be replaced by newArr.filter { $0 % newP != 0 }

Retrieve value from array inside of another array [C]

Take a look at this array:
const int *c000[64][1][3] =
{
//Row 1
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 2
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 3
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 4
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 5
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 6*
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 7
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
//Row 8
{ {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} }, { {0, 0, 0} },
};
Ignore the strange size and structure of the array, that's not what is important. I need to be able to use an array inside this array. For example, I have an array called h002:
const int h002[18] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0xe0, 0xe0}; //01
I need to be able to use h002 from inside c000. Such as:
{ {h002, 0, 0} },
This compiles just fine, however
myVar = c000[0][0][0];
retrieves the address of h002. I need the first item in h002, not the address. I'm confused as to why it would even give me the adress of h002 at all? I would imagine *h002 would retrieve the address, but then that doesn't compile at all.
I have seen several questions on SO that closely resemble mine, such as this one:
Creating an array of int arrays in C?
I have tried that particular example above. It works when I have it in my main.c file, but when I try it in the same .c file that c000 and h002 are contained in, it fails to compile. Perhaps this has something to do with it? I'm not sure why it would, considering using h002 inside c000 returns the address of h002 just fine. It's strange that the code presented in the link above wouldn't compile outside of my main file.
I feel like I'm making some kind of obvious, little mistake. I've been messing around with this off and on for about 5 days now. Trial and error and research has gotten me nowhere. Researching was difficult enough, as there doesn't seem to be much on using arrays like this, and none of my findings significantly helped me.
Feel free to ask questions. I'll be glad to specify more information if needed.
EDIT: Thanks so much to Stephan Lechner for helping me solve my issue. To get the result I needed, I had to do
myVar = *(c000[0][0][0]);
This works perfectly. I can also add whatever number I like at the end to retrieve different indexes in h002. For example:
myVar = *(c000[0][0][0] + 7);
Hope this helps someone out in the future.
I think the basic misunderstanding is the meaning of const int *c000[64][1][3], which denotes a three dimensional array of pointers to int, but not a pointer to an 3D-array of integers. To demonstrate this, consider the following simplified examples together with compiler warnings:
int aSingleIntValue = 10; // integer value
int *aSinglePtrToIntValue = &aSingleIntValue; // pointer to an integer value
// 1D-array of integer values
int oneDArrayOfInt[5] = { 3,4,5,6,7 };
// 1D-array of pointers to int; Note: OK, since 0 is interpreted as NULL
int *oneDArrayOfIntPtrOK[5] = { aSinglePtrToIntValue,0,0,0,0 };
// 1D-array of pointers to int; Note: 3,4,.. are int values, not pointers to int; Hence compiler complains:
// Warning: Incompatible integer to pointer conversion initializing "int *" with an expression of type "int"
int *oneDArrayOfIntPtrWarning[5] = { 3,4,5,6,7 };
// 3D-array of pointers to int; Note: OK, since 0 is interpreted as NULL
int *threeDArrayOfIntPtrOK[5][5][5] = { { { aSinglePtrToIntValue,0,0 }, { 0,0,0} } };
// 3D-array of pointers to int; Warining: Incompatible integer to pointer conversion initializing "int *" with an expression of type "int"
int *threeDArrayOfIntPtrWarning[5][5][5] = { { { 3,4,5 }, { 2,3,1} } };
Just for showing a different meaning: if one would like to declare a pointer to an array of 5 integers, this could be expressed as follows:
typedef int arrayOf5Int_t[5];
arrayOf5Int_t *arrayOf5IntPtr = &oneDArrayOfInt;
Given that, let's explain the meaning of
const int h002[18] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0xe0, 0xe0};
const int *c000[64][1][3] = { { { h002, 0, 0 } } };
Note that an element of c000 is a pointer to an integer, and note that variable h002 used in the context of the array initialiser decays to a pointer to an integer (pointing to the first element of h002). This is pretty much the same as declaring const int* ptrToH002 = h002, and the following print out demonstrates that this is actually the same:
const int *elemAt0x0x0 = c000[0][0][0];
const int *ptrToH002 = h002;
int isTheSame = elemAt0x0x0 == ptrToH002;
printf("pointer elemAt0x0x0 == ptrToH002 is %s", (isTheSame ? "true" : "false"));
// Prints: pointer elemAt0x0x0 == ptrToH002 is true
Hence, it should be clear that myVar = c000[0][0][0] retrieves an address, and not an integer value or an integer array. The address points to the first element of array h002.
Example of multidimensional array:
int main(){
int a1[] = {1,2,3,4};
int a2[] = {5,6,7,8};
int a3[] = {9,10,11,12};
int * superArr[3] = {a1,a2,a3};
printf("%d\n", superArr[2][1]);
}
You should be able to change your code to what you need using this example.

Trouble exiting 2D Array in C

I'm tyring to create a frequency counter. Basically, there's an array (testArray) with a bunch of numbers. I need to process those numbers and insert them in another array (probabilityArray). probabilityArray is also 2D array with the 1st row being the unique elements from the test array, the 2nd row being how many times a unique number occurs (Ex. probabilityArray[2][5] represents how often the number at probabilityArray[1][5] occurs). I'm having trouble exiting the rows for loop and I have no idea why.
#include <stdio.h>
#include <stdlib.h>
int histogram() {
}
int entropy() {
}
int main() {
int i, j, k, found = 0, currentPosition = 0, l = 0, x = 0, y = 0;
int testArray[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{11, 12, 13, 14, 15, 16, 17, 18 ,19, 20},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30}};
int row = sizeof(testArray) / sizeof(testArray[0]);
int col = sizeof(testArray[0]) / sizeof(testArray[0][0]);
int elements = (row * col);
printf("Elements: %d\n", elements);
//printf("Rows: %d\nCols: %d\n", row, col);
int probabilityArray[3][elements];
for(x = 0; x < 3; x++) {
for(y = 0; y < elements; y++) {
//printf("X: %d\tY: %d\t", x, y);
probabilityArray[x][y] = 0;
}
//printf("\n");
}
//printf("Got here\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d\n", l);
l++;
int temp = testArray[i][j];
for (k = 0; k < currentPosition; k++) {
if (probabilityArray[1][k] == temp) {
//Element is not unique, increase occurance counter
printf("NOT UNIQUE: %d\n", temp);
probabilityArray[2][k]++;
found = 1;
break;
}
}
if (found == 0) {
//Element is unique, add it to array
printf("FOUND: %d\n", temp);
probabilityArray[1][currentPosition] = temp;
probabilityArray[2][currentPosition]++;
currentPosition++;
printf("Current Position: %d\n", currentPosition);
}
found = 0;
}
printf("I: %d\tC: %d\n", i, j);
}
for (i = 0; i < currentPosition; i++) {
probabilityArray[3][i] = (int)((probabilityArray[2][i] / elements) * 100);
}
for (i = 0; i < currentPosition; i++) {
printf("ELEMENT: %d\t\tFREQUENCY: %d\t\tPROBABILITY:%d\n", probabilityArray[1][i], probabilityArray[2][i], probabilityArray[3][i]);
}
}
Any help is appreciated!
You are using indices 1, 2, and 3, where you probably meant to use 0, 1, and 2.
Here:
if (probabilityArray[1][k] == temp) { // Should it be 0??
//Element is not unique, increase occurance counter
printf("NOT UNIQUE: %d\n", temp);
probabilityArray[2][k]++; // Should it be 1??
and here:
probabilityArray[1][currentPosition] = temp; // Should it be 0??
probabilityArray[2][currentPosition]++; // Should it be 1??
and here:
probabilityArray[3][i] = (int)((probabilityArray[2][i] / elements) * 100);
// Should it be 2 and 1 ??
and here:
printf("ELEMENT: %d\t\tFREQUENCY: %d\t\tPROBABILITY:%d\n",
probabilityArray[1][i], probabilityArray[2][i], probabilityArray[3][i]);
// Should be be 0, 1, and 2
Fixing those might fix your problems.

Compiler dependency of searching a value in array of pointers to array of pointers in C

The following code have an array named ReadOnly[] which contains elements that are pointers to other arrays like AV_ReadOnly, BV_ReadOnly etc. Again AV_ReadOnly, BV_ReadOnly etc are pointer arrays containing elements that points to integer arrays.
The read_arrays() is a function used for printing a particular list/ accessing any particular value of the integer arrays. This approach works well on the test environment. But is there a chance for failure of this approach with a change in the platform/compiler?
#include<stdio.h>
int AV1_ReadOnly[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int AV2_ReadOnly[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int BV1_ReadOnly[] = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int BV2_ReadOnly[] = { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};
int MV1_ReadOnly[] = { 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
int MV2_ReadOnly[] = { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60};
int NC1_ReadOnly[] = { 61, 62, 63, 64, 65, 66, 67, 68, 69, 70};
int NC2_ReadOnly[] = { 71, 72, 73, 74, 75, 76, 77, 78, 79, 80};
int * AV_ReadOnly[] =
{
AV1_ReadOnly,
AV2_ReadOnly,
};
int * BV_ReadOnly[] =
{
BV1_ReadOnly,
BV2_ReadOnly,
};
int * MV_ReadOnly[] =
{
MV1_ReadOnly,
MV2_ReadOnly,
};
int * NC_ReadOnly[] =
{
NC1_ReadOnly,
NC2_ReadOnly
};
int ** ReadOnly[] =
{
AV_ReadOnly,
BV_ReadOnly,
MV_ReadOnly,
NC_ReadOnly
};
void read_arrays( int obj, int inst )
{
int ** ArrayPtr = ReadOnly[obj];
int count =0;
while( count <8 )
{
printf( "\n %d", *(ArrayPtr[inst]+count) );
count++;
}
}
void main()
{
read_arrays( 1,1 );
}
Should be OK as long as you keep the int arrays and the int* arrays in the same file.
Also, you can (should) declare them const if you have no intentions to change them.
Also, you can (should) declare them static if you have no intentions to extern them in other files.
BTW, from what you've published, it looks like you can simply use int table[8][10] instead...

Resources