1

I have this multidimensional array of strings in C and I want to access a specific value without doing a loop. Is it possible?

I've tried accessing the strings in the array using printf("Test %c", *pitchtomidi[0][4]) but I'm always only given the first character of the string and not the whole string. I've tried replacing %c with %s but I get this error:

"warning: format specifies type 'char ' but the argument has type 'char' [-Wformat]"

Here's the array:

char *pitchtomidi[104][8] = {
     {"24", "32.7", "B#0", "ty0", "C1", "do1", "Dbb1", "ru1"},
     {"25", "34.64444318554896", "C#1", "di1", "0", "0", "Db1", "ra1"},
     {"26", "36.7045089797165", "Cx1", "dy1", "D1", "re1", "Ebb1", "mu1"},
     {"27", "38.887072660588984", "D#1", "ri1", "0", "0", "Eb1", "me1"},
     {"28", "41.19941833156236", "Dx1", "ry1", "E1", "mi1", "Fb1", "fu1"},
     {"29", "43.64926323136014", "E#1", "my1", "F1", "fa1", "Gbb1", "su1"},
     {"30", "46.24478348960022", "F#1", "fi1", "0", "0", "Gb1", "se1"},
     {"31", "48.994641413867505", "Fx1", "fy1", "G1", "so1", "Abb1", "lu1"},
     {"32", "51.90801439936015", "G#1", "si1", "0", "0", "Ab1", "le1"},
     {"33", "54.99462555759296", "Gx1", "sy1", "A1", "la1", "Bbb1", "tu1"},
     {"34", "58.264776166378226", "A#1", "li1", "0", "0", "Bb1", "te1"},
     {"35", "61.7293800493828", "Ax1", "ly1", "B1", "ti1", "Cb2", "du2"},
     {"36", "65.40000000000005", "B#1", "ty1", "C2", "do2", "Dbb2", "ru2"},
     {"37", "69.28888637109796", "C#2", "di2", "0", "0", "Db2", "ra2"},
     {"38", "73.40901795943304", "Cx2", "dy2", "D2", "re2", "Ebb2", "mu2"},
     {"39", "77.77414532117801", "D#2", "ri2", "0", "0", "Eb2", "me2"},
     {"40", "82.39883666312477", "Dx2", "ry2", "E2", "mi2", "Fb2", "fu2"},
     {"41", "87.29852646272032", "E#2", "my2", "F2", "fa2", "Gbb2", "su2"},
     {"42", "92.48956697920049", "F#2", "fi2", "0", "0", "Gb2", "se2"},
     {"43", "97.98928282773505", "Fx2", "fy2", "G2", "so2", "Abb2", "lu2"},
     {"44", "103.81602879872034", "G#2", "si2", "0", "0", "Ab2", "le2"},
     {"45", "109.98925111518597", "Gx2", "sy2", "A2", "la2", "Bbb2", "tu2"},
     {"46", "116.5295523327565", "A#2", "li2", "0", "0", "Bb2", "te2"},
     {"47", "123.45876009876564", "Ax2", "ly2", "B2", "ti2", "Cb3", "du3"},
     {"48", "130.80000000000015", "B#2", "ty2", "C3", "do3", "Dbb3", "ru3"},
     {"49", "138.57777274219598", "C#3", "di3", "0", "0", "Db3", "ra3"},
     {"50", "146.81803591886614", "Cx3", "dy3", "D3", "re3", "Ebb3", "mu3"},
     {"51", "155.54829064235608", "D#3", "ri3", "0", "0", "Eb3", "me3"},
     {"52", "164.7976733262496", "Dx3", "ry3", "E3", "mi3", "Fb3", "fu3"},
     {"53", "174.5970529254407", "E#3", "my3", "F3", "fa3", "Gbb3", "su3"},
     {"54", "184.97913395840104", "F#3", "fi3", "0", "0", "Gb3", "se3"},
     {"55", "195.97856565547016", "Fx3", "fy3", "G3", "so3", "Abb3", "lu3"},
     {"56", "207.63205759744073", "G#3", "si3", "0", "0", "Ab3", "le3"},
     {"57", "219.978502230372", "Gx3", "sy3", "A3", "la3", "Bbb3", "tu3"},
     {"58", "233.05910466551305", "A#3", "li3", "0", "0", "Bb3", "te3"},
     {"59", "246.91752019753133", "Ax3", "ly3", "B3", "ti3", "Cb4", "du4"},
     {"60", "261.60000000000036", "B#3", "ty3", "C4", "do4", "Dbb4", "ru4"},
     {"61", "277.15554548439206", "C#4", "di4", "0", "0", "Db4", "ra4"},
     {"62", "293.63607183773246", "Cx4", "dy4", "D4", "re4", "Ebb4", "mu4"},
     {"63", "311.0965812847124", "D#4", "ri4", "0", "0", "Eb4", "me4"},
     {"64", "329.5953466524994", "Dx4", "ry4", "E4", "mi4", "Fb4", "fu4"},
     {"65", "349.1941058508816", "E#4", "my4", "F4", "fa4", "Gbb4", "su4"},
     {"66", "369.95826791680236", "F#4", "fi4", "0", "0", "Gb4", "se4"},
     {"67", "391.9571313109406", "Fx4", "fy4", "G4", "so4", "Abb4", "lu4"},
     {"68", "415.26411519488175", "G#4", "si4", "0", "0", "Ab4", "le4"},
     {"69", "439.95700446074426", "Gx4", "sy4", "A4", "la4", "Bbb4", "tu4"},
     {"70", "466.1182093310264", "A#4", "li4", "0", "0", "Bb4", "te4"},
     {"71", "493.83504039506295", "Ax4", "ly4", "B4", "ti4", "Cb5", "du5"},
     {"72", "523.200000000001", "B#4", "ty4", "C5", "do5", "Dbb5", "ru5"},
     {"73", "554.3110909687844", "C#5", "di5", "0", "0", "Db5", "ra5"},
     {"74", "587.2721436754651", "Cx5", "dy5", "D5", "re5", "Ebb5", "mu5"},
     {"75", "622.193162569425", "D#5", "ri5", "0", "0", "Eb5", "me5"},
     {"76", "659.1906933049991", "Dx5", "ry5", "E5", "mi5", "Fb7", "fu7"},
     {"77", "698.3882117017636", "E#5", "my5", "F5", "fa5", "Gbb5", "su5"},
     {"78", "739.916535833605", "F#5", "fi5", "0", "0", "Gb5", "se5"},
     {"79", "783.9142626218817", "Fx7", "fy7", "G5", "so5", "Abb5", "lu5"},
     {"80", "830.5282303897641", "G#5", "si5", "0", "0", "Ab5", "le5"},
     {"81", "879.9140089214892", "Gx5", "sy5", "0", "0", "A5", "la5"},
     {"82", "932.2364186620536", "Bbb5", "tu5", "A#5", "li5", "Bb5", "te5"},
     {"83", "987.6700807901268", "Ax5", "ly5", "B5", "ti5", "Cb6", "du6"},
     {"84", "1046.400000000003", "B#5", "ty5", "C6", "do6", "Dbb6", "ru6"},
     {"85", "1108.6221819375698", "C#6", "di6", "0", "0", "Db6", "ra6"},
     {"86", "1174.5442873509314", "Cx6", "dy6", "D6", "re6", "Ebb6", "mu6"},
     {"87", "1244.3863251388511", "D#6", "ri6", "0", "0", "Eb6", "me6"},
     {"88", "1318.3813866099995", "Dx6", "ry6", "E6", "mi6", "Fb6", "fu6"},
     {"89", "1396.7764234035285", "E#6", "my6", "F6", "fa6", "Gbb6", "su6"},
     {"90", "1479.8330716672115", "F#6", "fi6", "0", "0", "Gb6", "se6"},
     {"91", "1567.8285252437647", "Fx6", "fy6", "G6", "so6", "Abb6", "lu6"},
     {"92", "1661.0564607795295", "G#6", "si6", "0", "0", "Ab6", "le6"},
     {"93", "1759.8280178429798", "Gx6", "sy6", "A6", "la6", "Bbb6", "tu6"},
     {"94", "1864.4728373241085", "A#6", "te6", "0", "0", "Bb6", "te6"},
     {"95", "1975.340161580255", "Ax6", "ly6", "B6", "ti6", "Cb7", "du7"},
     {"96", "2092.8000000000075", "B#6", "ty6", "C7", "do7", "Dbb7", "ru7"},
     {"97", "2217.244363875141", "C#7", "di7", "0", "0", "Db7", "ra7"},
     {"98", "2349.088574701864", "Cx7", "dy7", "D7", "re7", "Ebb7", "mu7"},
     {"99", "2488.7726502777036", "D#7", "ri7", "0", "0", "Eb7", "me7"},
     {"100", "2636.7627732200003", "Dx7", "ry7", "E7", "mi7", "Fb7", "fu7"},
     {"101", "2793.5528468070584", "E#7", "my7", "F7", "fa7", "Gbb7", "su7"},
     {"102", "2959.6661433344243", "F#7", "fi7", "0", "0", "Gb7", "se7"},
     {"103", "3135.657050487531", "Fx7", "fy7", "G7", "so7", "Abb7", "lu7"},
     {"104", "3322.1129215590604", "G#7", "si7", "0", "0", "Ab7", "le7"},
     {"105", "3519.656035685961", "Gx7", "sy7", "A7", "la7", "Bbb7", "tu7"},
     {"106", "3728.9456746482183", "A#7", "li7", "0", "0", "Bb7", "te7"},
     {"107", "3950.6803231605113", "Ax7", "ly7", "B7", "ti7", "Cb8", "du8"},
     {"108", "4185.600000000016", "B#7", "ty7", "C8", "do8", "Dbb8", "ru8"},
     {"109", "4434.488727750283", "C#8", "di8", "0", "0", "Db8", "ra8"},
     {"110", "4698.177149403729", "Cx8", "dy8", "D8", "re8", "Ebb8", "mu8"},
     {"111", "4977.545300555408", "D#8", "ri8", "0", "0", "Eb8", "me8"},
     {"112", "5273.525546440002", "Dx8", "ry8", "E8", "mi8", "Fb8", "fu8"},
     {"113", "5587.105693614118", "E#8", "my8", "F8", "fa8", "Gbb8", "su8"},
     {"114", "5919.3322866688495", "F#8", "fi8", "0", "0", "Gb8", "se8"},
     {"115", "6271.3141009750625", "Fx8", "fy8", "G8", "so8", "Abb8", "lu8"},
     {"116", "6644.225843118122", "G#8", "si8", "0", "0", "Ab8", "le8"},
     {"117", "7039.312071371923", "Gx8", "sy8", "A8", "la8", "Bbb8", "tu8"},
     {"118", "7457.8913492964375", "A#8", "li8", "0", "0", "Bb8", "te8"},
     {"119", "7901.3606463210235", "Ax8", "ly8", "B8", "ti8", "Cb9", "du9"},
     {"120", "8371.200000000033", "B#8", "ty8", "C9", "do9", "Dbb9", "ru9"},
     {"121", "8868.977455500568", "C#9", "di9", "0", "0", "Db9", "ra9"},
     {"122", "9396.35429880746", "Cx9", "dy9", "D9", "re9", "Ebb9", "mu9"},
     {"123", "9955.090601110818", "D#9", "ri9", "0", "0", "Eb9", "me9"},
     {"124", "10547.051092880005", "Dx9", "ry9", "E9", "mi9", "Fb9", "fu9"},
     {"125", "11174.211387228237", "E#9", "my9", "F9", "fa9", "Gbb9", "su9"},
     {"126", "11838.664573337703", "F#9", "fi9", "0", "0", "Gb9", "se9"},
     {"127", "12542.628201950129", "Fx9", "fy9", "G9", "so9", "Abb9", "lu9"}
};

Thanks!

5
  • 3
    %s is right, but *pitchtomidi[0][4] should be pitchtomidi[0][4] without the * Commented Jun 19, 2019 at 8:03
  • Thank you! That's the answer. Commented Jun 19, 2019 at 8:07
  • 3
    Unrelated to the question, you should change the declaration to const char *pitchtomidi[104][8]. Commented Jun 19, 2019 at 8:12
  • @Lundin thanks, I made the change. I presume it's to be safe in case I unintentionally override the values when they are meant to remain the same. Commented Jun 19, 2019 at 8:14
  • 1
    @NakulTiruviluamala It needs to be const since you can't change the values, they are all read-only string literals. Commented Jun 19, 2019 at 8:18

1 Answer 1

1

As Blaze pointed out, %s is right, but *pitchtomidi[0][4] should be pitchtomidi[0][4] without the *

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.