Why does printing a 2D array of characters give garbage values? - c

I am implementing a function that prints a 2D array of characters only using a double-pointer and pointer notation. When I run the code, it prints a bunch of garbage values in the format I want instead of the correct characters.
My professor instructed me not to use arr[row][col], instead, I must access it using ((arr+i)+j) or similar
This is a project for a class and I can't change any of the code outside of this function. The characters are meant to be formatted like a word search puzzle. The arguments passed to my function are char** arr, int size.
This is my function:
for(int i = 0; i < size; i++){
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(arr+i), *(arr+i)+1, *(arr+i)+2, *(arr+i)+3, *(arr+i)+4, *(arr+i)+5, *(arr+i)+6, *
(arr+i)+7, *(arr+i)+8, *(arr+i)+9, *(arr+i)+10, *(arr+i)+11, *(arr+i)+12, *(arr+i)+13, *(arr+i)+14 ); }
Expected output:
W D B M J Q D B C J N Q P T I
I R Z U X U Z E A O I O R T N
M N Z P L R N H L Y L X H M D
M Y E K A I D P I U L Y O W I
A O A B A R K U F V I H L A A
L O N M R X K I O J N A V R N
A E P T A A R A R T O W A I A
S U C Z A U S I N A I A L Z V
K O T A O N R K I S S I A O N
A H X S V K A I A E A I B N E
U D S X N X C C D W G S A A V
O I S D W L E J N J T X M H A
M O X W T N H Q D X O Q A Q D
R U U V G E O R G I A Q V D A
V F L O R I D A L G L W O X N
Actual output:
░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛
≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■
0 1 2 3 4 5 6 7 8 9 : ; < = >
P Q R S T U V W X Y Z [ \ ] ^
p q r s t u v w x y z { | } ~
É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧
░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛
╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐
≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■
0 1 2 3 4 5 6 7 8 9 : ; < = >
P Q R S T U V W X Y Z [ \ ] ^
└ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬
` a b c d e f g h i j k l m n

If arr is really a char**, then you need to dereference twice to get a char.
So, in your statement, arr+i is another char**, pointing at a char* i steps further along from the one arr points at. Hopefully arr points at the beginning of an array of char* at least size long.
Now *(arr+i) dereferences it, fetching the char* pointed to by arr+i, giving you a char*.
Now *(arr+i)+7, for example, is another char*, pointing at a char 7 steps further along from the one *(arr+i) points at. Hopefully *(arr+i) points at the beginning of an array of char at least 15 long.
But you don't dereference it, so you're attempting to print the value of the pointer (i.e. the address it holds), not the value it points to (the char).
Try *(*(arr+i)+7).

Related

Join four columns into one according to each row

A B C D
E F G H
I J K L
M N O P
If I chose to join the columns I would ={A1:A;B1:B;C1:C;D1:D} but it would look like this:
A
E
I
M
B
F
J
N
... and so on
I would like it to look like this:
A
B
C
D
E
F
G
... and so on
How to proceed in this case?
Note: It may happen that some of the columns are not complete in data, some may have more values than the others, but I still want to continue following this same pattern. Example:
A B D
E G H
I J K L
M N O P
Result:
A
B
D
E
G
H
... and so on
use:
=TRANSPOSE(QUERY(TRANSPOSE(A:D),, 9^9))
then:
=TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(A:D),,9^9)),,9^9), " "))

How to get character like go down stairs in Google Sheets

I want to get a character from string but the path is like going downstairs in Google Sheets
The string is like:
y C I 6 8 V 5
~5 Z n I L w f
V ~s i w J d _
o R ~4 2 i v f
9 ^ j ~h r u #
Z y Q 7 ~1 u a
T t z u _ ~! Q
G Y n r * t ~^
J A l v F j d
a 2 l - y O B
h B w % n a 4
M t _ P D W a
And expect the output is:
5 s 4 h 1 ! ^
I put ~ in front of the character that demo the path I want to get.
The logic is I will find the first column which cell is 5 and then get start from that cell.
Example sheet link
https://docs.google.com/spreadsheets/d/1UQEGEl_rqAMFePDAGoueTI47xF8T_DyrvJ3de5pkLRA/edit#gid=306981885
I tried auto-fill but it could only incrementally either row or column. And I hope to incremental both row and column.
={INDIRECT("A"&MATCH(5, A1:A14, 0)),
INDIRECT("B"&MATCH(5, A1:A14, 0)+1),
INDIRECT("C"&MATCH(5, A1:A14, 0)+2),
INDIRECT("D"&MATCH(5, A1:A14, 0)+3),
INDIRECT("E"&MATCH(5, A1:A14, 0)+4),
INDIRECT("F"&MATCH(5, A1:A14, 0)+5),
INDIRECT("G"&MATCH(5, A1:A14, 0)+6)}
for a large scale scenario you can use this one and drag it to the right:
=INDIRECT(ADDRESS(MATCH(5, $A1:$A14, 0)+COLUMN()-1, COLUMN(), 4))
You can also do it with an array formula if you want to:
=ArrayFormula(hlookup(column(A2:G13),{column(A2:G13);A2:G13},match(5,A2:A13,0)+column(A2:G13)))

Remove spaces between characters of output Batch file

I have a batch file that outputs the Wi-Fi adapter MAC address to a text file using the command wmic nic where "name like '%%802%%'" get name,macaddress and there are spaces between characters. I get the same thing for sfc /scannow in a batch file.
The output of the wmic command is:
Hostname: SOME-COMPUTER
A C A d d r e s s N a m e
3 4 : F 6 : B 3 : J 3 : 6 3 : 1 3 B r o a d c o m 8 0 2 . 1 1 n N e t w o r k A d a p t e r
The snippet of output of the sfc scan is:
n 3 0 % c o m p l e t e . V e r i f i c a t i o n 3 1 % c o m p l e t e . V e r i f i c a t i o n 3 1 % c o m p l e t e . V e r i f i c a t i o n 3 2 % c o m p l e t e . V e r i f i c a t i o n 3 2 % c o m p l e t e . V e r i f i c a t i o n 3 3 % c o m p l e t e . V e r i f i c a t i o n 3 3 % c o m p l e t e . V e r i f i c a t i o n 3 4 % c o m p l e t e . V e r i f i c a t i o n 3 4 % c o m p l e t e . V e r i f i c a t i o n 3 5 % c o m p l e t e . V e r i f i c a t i o n 3 5 % c o m p l e t e . V e r i f i c a t i o n 3 5 % c o m p l e t e . V e r i f i c a t i o n 3 6 % c o m p l e t e . V e r i f i c a t i o n 3 6 % c o m p l e t e . V e r i f i c a t i o n 3 7 % c o m p l e t e . V e r i f i c a t i o n 3 7 % c o m p l e t e . V e r i f i c a t i o n 3 8 % c o m p l e t e . V e r i f i c a t i o n 3 8 % c o m p l e t e . V e r i f i c a t i o n 3 9 % c o m p l e t e . V e r i f i c a t i o n 3 9 % c o m p l e t e . V e r i f i c a t i o n 4 0 % c o m p l e t e . V e r i f i c a t i o n 4 0 % c o m p l e t e . V e r i f i c a t i o n 4 1 % c o m p l e t e . V e r i f i c a t i o n 4 1 % c o m p l e t e . V e r i f i c a t i o n 4 2 % c o m p l e t e . V e r i f i c a t i o n 4 2 % c o m p l e t e . V e r i f i c a t i o n 4 2 % c o m p l e t e . V e r i f i c a t i o n 4 3 % c o m p l e t e . V e r i f i c a t i o n 4 3 % c o m p l e t e . V e r i f i c a t i o n 4 4 % c o m p l e t e . V e r i f i c a t i o n 4 4 % c o m p l e t e . V e r i f i c a t i o n 4 5 % c o m p l e t e . V e r i f i c a t i o n 4 5 % c o m p l e t e . V e r i f i c a t i o n 4 6 % c o m p l e t e . V e r i f i c a t i o n 4 6 % c o m p l e t e . V e r i f i c a t i o n 4 7 % c o m p l e t e . V e r i f i c a t i o n 4 7 % c o m p l e t e . V e r i f i c a t i o n 4 8 % c o m p l e t e . V e r i f i c a t i o n 4 8 % c o m p l e t e . V e r i f i c a t i o n 4 9 % c o m p l e t e . V e r i f i c a t i o n 4 9 % c o m p l e t e . V e r i f i c a t i o n 5 0 % c o m p l e t e . V e r i f i c a t i o n 5 0 % c o m p l e t e . V e r i f i c a t i o n 5 0 % c o m p l e t e . V e r i f i c a t i o n 5 1 % c o m p l e t e . V e r i f i c a t i o n 5 1 % c o m p l e t e . V e r i f i c a t i o n 5 2 % c o m p l e t e . V e r i f i c a t i o n 5 2 % c o m p l e t e . V e r i f i c a t i o n 5 3 % c o m p l e t e . V e r i f i c a t i o n 5 3 % c o m p l e t e . V e r i f i c a t i o n 5 4 % c o m p l e t e . V e r i f i c a t i o n 5 4 % c o m p l e t e . V e r i f i c a t i o n 5 5 % c o m p l e t e . V e r i f i c a t i o n 5 5 % c o m p l e t e . V e r i f i c a t i o n 5 6 % c o m p l e t e . V e r i f i c a t i o n 5 6 % c o m p l e t e . V e r i f i c a t i o n 5 7 % c o m p l e t e . V e r i f i c a t i o n 5 7 % c o m p l e t e . V e r i f i c a t i o n 5 7 % c o m p l e t e . V e r i f i c a t i o n 5 8 % c o m p l e t e . V e r i f i c a t i o n 5 8 % c o m p l e t e . V e r i f i c a t i o n 5 9 % c o m p l e t e . V e r i f i c a t i o n 5 9 % c o m p l e t e . V e r i f i c a t i o n 6 0 % c o m p l e t e . V e r i f i c a t i o n 6 0 % c o m p l e t e . V e r i f i c a t i o n 6 1 % c o m p l e t e . V e r i f i c a t i o n 6 1 % c o m p l e t e . V e r i f i c a t i o n 6 2 % c o m p l e t e . V e r i f i c a t i o n 6 2 % c o m p l e t e . V e r i f i c a t i o n 6 3 % c o m p l e t e . V e r i f i c a t i o n 6 3 % c o m p l e t e . V e r i f i c a t i o n 6 4 % c o m p l e t e . V e r i f i c a t i o n 6 4 % c o m p l e t e . V e r i f i c a t i o n 6 4 % c o m p l e t e . V e r i f i c a t i o n 6 5 % c o m p l e t e . V e r i f i c a t i o n 6 5 % c o m p l e t e . V e r i f i c a t i o n 6 6 % c o m p l e t e . V e r i f i c a t i o n 6 6 % c o m p l e t e . V e r i f i c a t i o n 6 7 % c o m p l e t e . V e r i f i c a t i o n 6 7 % c o m p l e t e . V e r i f i c a t i o n 6 8 % c o m p l e t e . V e r i f i c a t i o n 6 8 % c o m p l e t e . V e r i f i c a t i o n 6 9 % c o m p l e t e . V e r i f i c a t i o n 6 9 % c o m p l e t e . V e r i f i c a t i o n 7 0 % c o m p l e t e . V e r i f i c a t i o n 7 0 % c o m p l e t e . V e r i f i c a t i o n 7 1 % c o m p l e t e . V e r i f i c a t i o n 7 1 % c o m p l e t e . V e r i f i c a t i o n 7 1 % c o m p l e t e . V e r i f i c a t i o n 7 2 %
When I run these commands from cmd.exe, the output looks like this:
MACAddress Name
34:F2:B3:A1:83:33 Broadcom 802.11n Network Adapter
I would use PowerShell normally, but that is not what I need here. I would like the output to look like this in the output file.
The output doesn't seem to care if I use setlocal enabledelayedexpansion endlocal, as the output is the same.
Thank you for your help!
EDIT
I created a blank Unicode file ( I have Windows 10 ), and copied it to a new file I used for the output file. Now, the spaces are gone, but for whatever reason, the variable created for the hostname outputs what appears to be Chinese characters:
潈瑳慮敭›䕄䭓佔ⵐ䩒䵔剄⁓਍MACAddress Name
34:F2:B3:A1:83:33 Broadcom 802.11n Network Adapter
The hostname command was used as follows:
for /f "delims= tokens=*" %%i in ('hostname') do (
set "nameHost=%%i"
)
Then, I changed tokens=* to tokens=2 and all of the characters were Chinese. Anyway, a work-around was to add the hostname to the filename instead, which resolves this issue. However, it would be ideal to learn how to have both ANSI and Unicode strings in the same file, without the spaces.
I saw a C++ article about this and the answer was to change the code; therefore, if that it true, I don't have the code for sfc or wmic, so am I SOL there, or is there a way to have both ANSI and Unicode strings in the same file without the spaces between each character?
wmic nic where "name like '%%802%%'" get name,macaddress|more
should provide you with an ANSI version - the problem there is Unicode-output from WMIC.

SaveToFile and then open it formating text weird

Okay so I am writing a program that imports a database to an text file Via (SaveToFile command) .But when I open the file normally it gives me.
TG! ¶’ò?²Ï# ª _þX g Ò­cöëÏ°ã ª ? Á<Ž¶ëmÐö ª _þX
| ¾"µÈó\Î­å ª Dw= ÿÿ† ÿÿ" I Á<Ž¶ëmÐö ª _þX 2 . " C l i e n t s " C l i e n t s + ð I D I D
ÿ Z ÿÿÿÿC ð S u r e n a m e S u r e n a m e ‚ ÿ ÿ j ÿÿC ð P a s s w o r d P a s s w o r d
ÿ z ÿÿ3 ð N a m e N a m e ‚ ÿ ÿ j ÿÿK ð
M o n e y P a i d
M o n e y P a i d ÿ z ÿÿK ð
M o n e y O w e d
M o n e y O w e d ÿ z ÿÿ[ ð O n c e O f f C l i e n t O n c e O f f C l i e n t ÿ ÿ Z ÿÿC ð P h o n e I D P h o n e I D
ÿ z ÿÿÿ a w e a w e Ó–I
Here is my code:
procedure TfrmRawDATA.btnStoreFeedClick(Sender: TObject);
var
StoreFeed : string;
StoreFeedFile: TextFile;
data : string;
begin
begin
if (FileExists('C:\Users\ASROCK\Desktop\IT-PAT 2014\PAT Fase 3\StoreFeedFile.txt')) then
begin
DeleteFile('C:\Users\ASROCK\Desktop\IT-PAT 2014\PAT Fase 3\StoreFeedFile.txt');
ShowMessage('Save file deleted!');
end
else
AssignFile(StoreFeedFile,'Test.txt');
FileSetAttr('C:\Users\ASROCK\Desktop\IT-PAT 2014\PAT Fase 3\StoreFeedFile.txt', faReadOnly);
dmMJCPlus.tblClients.SaveToFile('C:\Users\ASROCK\Desktop\IT-PAT 2014\PAT Fase 3\StoreFeedFile.txt');
end;
end;
I just wanna know how to set like the file type or something so it doesn't give me that text.
The default saving format for ADO table/query and ClientDataSet is binary. You have the option of using XML though. You need to specify it in the call to SaveToFile:
ClientDataSet.SaveToFile('...', dfXML);
or
ADOTable.SaveToFile('...', pfXML);
Having a file extension of '.xml' should achieve the same, looking at the source, though it would seem it didn't turn out to be the case for you (as you seem to have tried it in the comments).
pfXML/dfXML are defined in 'adodb.pas' and 'dbclient.pas' respectively.

C reading csv file

I'm running into a problem I haven't encountered before and am baffled... for some reason when I try to read a CSV file char by char but it seems like spaces are somehow getting placed there... and what's weirder is the fact that no space chars exist anywhere. I will give an example...
char *readgd(const char *fname)
{
char *gddata, *tmp;
FILE *fp;
int buff = 1024, c = 0, ch;
if(!(fp = fopen(fname, "r")))
{
printf("\nError! Could not open %s!", fname);
return 0x00;
}
if(!(gddata = malloc(buff)))
{
fclose(fp);
printf("\nError! Memory allocation failed!");
return 0x00;
}
while(ch != EOF)
{
c++;
ch = fgetc(fp);
if(buff <= c)
{
buff += buff;
if(!(tmp = realloc(gddata, buff)))
{
free(gddata);
fclose(fp);
printf("\nError! Memory allocation failed!");
}
gddata = tmp;
}
gddata[c - 1] = ch;
if(gddata[c - 1] != ' ') printf("%c", gddata[c - 1]); //no spaces?
}
if(!(tmp = realloc(gddata, c + 1)))
{
free(gddata);
fclose(fp);
printf("\nError! Memory allocation failed!");
}
gddata = tmp;
gddata[c] = 0x00;
fclose(fp);
return gddata;
}
with the following CSV snippet:
:Tagname,Area,SecurityGroup,Container,ContainedName,ShortDesc,ExecutionRelativeOrder,ExecutionRelatedObject,UDAs,Extensions,CmdData,Address_ACbHAlmCfg,Address_ACbHWarnCfg,Address_ACbLAlmCfg,Address_ACbLWarnCfg,Address_ACbTfCfg,Address_ACrHAlmDb,Address_ACrHAlmSp,Address_ACrHAlmTmrSp,Address_ACrHWarnDb,Address_ACrHWarnSp,Address_ACrHWarnTmrSp,Address_ACrLAlmDb,Address_ACrLAlmSp,Address_ACrLAlmTmrSp,Address_ACrLWarnDb,Address_ACrLWarnSp,Address_ACrLWarnTmrSp,Address_ACrTfTmrSp,Address_bHalm,Address_bHWarn,Address_bLAlm,Address_bLwarn,Address_bMode,Address_bTfAlm,Address_rCCmd,Address_rVal,
outputs this onto the console:
 
■: T a g n a m e , A r e a , S e c u r i t y G r o u p , C o n t a i n e r , C
o n t a i n e d N a m e , S h o r t D e s c , E x e c u t i o n R e l a t i v e
O r d e r , E x e c u t i o n R e l a t e d O b j e c t , U D A s , E x t e n s
i o n s , C m d D a t a , A d d r e s s _ A C b H A l m C f g , A d d r e s s _
A C b H W a r n C f g , A d d r e s s _ A C b L A l m C f g , A d d r e s s _ A
C b L W a r n C f g , A d d r e s s _ A C b T f C f g , A d d r e s s _ A C r H
A l m D b , A d d r e s s _ A C r H A l m S p , A d d r e s s _ A C r H A l m T
m r S p , A d d r e s s _ A C r H W a r n D b , A d d r e s s _ A C r H W a r n
S p , A d d r e s s _ A C r H W a r n T m r S p , A d d r e s s _ A C r L A l m
D b , A d d r e s s _ A C r L A l m S p , A d d r e s s _ A C r L A l m T m r S
p , A d d r e s s _ A C r L W a r n D b , A d d r e s s _ A C r L W a r n S p ,
A d d r e s s _ A C r L W a r n T m r S p , A d d r e s s _ A C r T f T m r S p
, A d d r e s s _ b H a l m , A d d r e s s _ b H W a r n , A d d r e s s _ b L
A l m , A d d r e s s _ b L w a r n , A d d r e s s _ b M o d e , A d d r e s s
_ b T f A l m , A d d r e s s _ r C C m d , A d d r e s s _ r V a l ,
I am very confused as to where these spaces are coming from. Any help would be greatly appreciated.
Are you sure the CSV is not encoded with UTF-16 (using two bytes per character)?
This is the most likely reason you'd see spaces between otherwise valid ASCII characters, so try verifying the encoding first.

Resources