Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C] How is data in an array passed?
New Posts  All Forums:Forum Nav:

[C] How is data in an array passed?

post #1 of 6
Thread Starter 
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_array(char character);

main()
{
        int i;
        int loop_length;

        char output[] = "Hello World\n\n";
        loop_length = strlen(output);

        for (i = 0; i < loop_length; i++)
        {
                print_array(output[i]);
        }

        printf("Program completed.  Press any key to continue...\n");
        getchar();
        return 0;
}

void print_array(int character)
{
        printf("%d\n", character);
}

I'm reviewing C for my own benefit since I'm taking a course that's focusing on embedded C with microcontrollers. Output is just the ASCII values of the characters in the Hello World string.

Inside the for loop, I'm passing "output[ i]" to test. What exactly is passed when I say "print_array(output[ i ])"? An address (like a pointer would)? A value?
Edited by stn0092 - 4/5/13 at 8:38pm
LancelotX79
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K @ 4.2 GHz ASRock Extreme 6 X79 AMD Radeon HD 7970 Crossfire 16 GB Samsung (1866 MHz, 9-9-10-24-84 1T) 
Hard DriveHard DriveOptical DriveCooling
2x 128 GB Samsung 830 2x 1 TB WD Black, 1 TB WD Green Samsung Writemaster DVD Corsair H80, 2x GT AP-15 
OSMonitorMonitorKeyboard
Windows 7 Professional x64 Yamakasi Q271 (2560x1440) Samsung 2493HM (1920x1200) Ducky Shine 3 YOTS Black 
PowerCaseMouseAudio
Corsair HX1000W Lian Li A70B Logitech G9 ASUS Xonar DSX 
  hide details  
Reply
LancelotX79
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K @ 4.2 GHz ASRock Extreme 6 X79 AMD Radeon HD 7970 Crossfire 16 GB Samsung (1866 MHz, 9-9-10-24-84 1T) 
Hard DriveHard DriveOptical DriveCooling
2x 128 GB Samsung 830 2x 1 TB WD Black, 1 TB WD Green Samsung Writemaster DVD Corsair H80, 2x GT AP-15 
OSMonitorMonitorKeyboard
Windows 7 Professional x64 Yamakasi Q271 (2560x1440) Samsung 2493HM (1920x1200) Ducky Shine 3 YOTS Black 
PowerCaseMouseAudio
Corsair HX1000W Lian Li A70B Logitech G9 ASUS Xonar DSX 
  hide details  
Reply
post #2 of 6
Quote:
Originally Posted by stn0092 View Post

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void print_array(char character);

main()
{
        int i;
        int loop_length;

        char output[] = "Hello World\n\n";
        loop_length = strlen(output);

        for (i = 0; i < loop_length; i++)
        {
                print_array(output[i]);
        }

        printf("Program completed.  Press any key to continue...\n");
        getchar();
        return 0;
}

void print_array(int character)
{
        printf("%d\n", character);
}

I'm reviewing C for my own benefit since I'm taking a course that's focusing on embedded C with microcontrollers. Output is just the ASCII values of the characters in the Hello World string.

Inside the for loop, I'm passing "output[ i]" to test. What exactly is passed when I say "print_array(output[ i ])"? An address (like a pointer would)? A value?

output[ i ] is passed by value to the callee print_array. If you like pointers, this might make more sense (output[ i ] is basically this in disguise):
Code:
print_array( *(output+(sizeof(char)*i) );

This would pass by reference (pointer):
Code:
print_array( &output[i] ); // pass by ref

// pass by ref (pointer)
void print_array( int *character )
{
        printf( "%d\n", *character );
}
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #3 of 6
Since you're going to be in an embedded C course, I'd suggest you first learn what bit size the controller is going to be so you can begin to convert your coding habits into ones better suited for an embedded environment.

As for your question, C ALWAYS passes by value. That is the nature of the language. Even when using pointers, you're passing by value (the value just happens to represent a memory location).
Router
(12 items)
 
  
Reply
Router
(12 items)
 
  
Reply
post #4 of 6
Thread Starter 
I have another question.
Code:
char *ptr1 = "HELLO";
char *ptr2 = "WORLD";

printf("%d\n", ptr1);
printf("%d\n", ptr2);


So, two pointers each initialized to a string constant. The printf statements outputs the values of ptr1 and ptr2; those values are the address of each string constant.

How are the string constants stored? How much memory does a string constant take up? I noticed that every time I run this, ptr1 and ptr2 differ by eight; seems like it moves by four for every three characters. If ptr1 is 0x457C8, then ptr2 is 0x457C0.
LancelotX79
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K @ 4.2 GHz ASRock Extreme 6 X79 AMD Radeon HD 7970 Crossfire 16 GB Samsung (1866 MHz, 9-9-10-24-84 1T) 
Hard DriveHard DriveOptical DriveCooling
2x 128 GB Samsung 830 2x 1 TB WD Black, 1 TB WD Green Samsung Writemaster DVD Corsair H80, 2x GT AP-15 
OSMonitorMonitorKeyboard
Windows 7 Professional x64 Yamakasi Q271 (2560x1440) Samsung 2493HM (1920x1200) Ducky Shine 3 YOTS Black 
PowerCaseMouseAudio
Corsair HX1000W Lian Li A70B Logitech G9 ASUS Xonar DSX 
  hide details  
Reply
LancelotX79
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K @ 4.2 GHz ASRock Extreme 6 X79 AMD Radeon HD 7970 Crossfire 16 GB Samsung (1866 MHz, 9-9-10-24-84 1T) 
Hard DriveHard DriveOptical DriveCooling
2x 128 GB Samsung 830 2x 1 TB WD Black, 1 TB WD Green Samsung Writemaster DVD Corsair H80, 2x GT AP-15 
OSMonitorMonitorKeyboard
Windows 7 Professional x64 Yamakasi Q271 (2560x1440) Samsung 2493HM (1920x1200) Ducky Shine 3 YOTS Black 
PowerCaseMouseAudio
Corsair HX1000W Lian Li A70B Logitech G9 ASUS Xonar DSX 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by stn0092 View Post

I have another question.
Code:
char *ptr1 = "HELLO";
char *ptr2 = "WORLD";

printf("%d\n", ptr1);
printf("%d\n", ptr2);


So, two pointers each initialized to a string constant. The printf statements outputs the values of ptr1 and ptr2; those values are the address of each string constant.

How are the string constants stored? How much memory does a string constant take up? I noticed that every time I run this, ptr1 and ptr2 differ by eight; seems like it moves by four for every three characters. If ptr1 is 0x457C8, then ptr2 is 0x457C0.

A String in C is just an array of characters with a null termination byte at the end (0x00). So, a String will take up (number of characters + 1) * sizeof(char) bytes of memory, which will typically be number of characters + 1 bytes. The +1 is for the null termination.

In your example, HELLO should be stored as 48 45 4C 4C 4F 00. The first five bytes are the ascii representations of H E L L O, and the 00 is the null termination, which designates the end of the string.

This is just an ideal situation though, and in practice, it's often better to align memory differently depending on the architecture. So, in your example, each String only needs 6 bytes of memory to be stored (five characters + null terminatino), but is actually stored in 8 bytes. This is just the compiler deciding that it will be more efficient to align the memory in 4/8 byte chunks. Depending on the architecture, it may be more efficient to do this.

As for the location, I believe this is going to depend on the compiler, but the actual strings are probably stored in the .data section of memory. The pointers will be on the stack and point to the .data section of memory where the Strings exist.
Edited by Waffleboy - 4/7/13 at 3:42am
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
post #6 of 6
Quote:
Originally Posted by Waffleboy View Post

As for the location, I believe this is going to depend on the compiler, but the actual strings are probably stored in the .data section of memory. The pointers will be on the stack and point to the .data section of memory where the Strings exist.
Close. The memory allotted to each process is actually more controlled by the kernel through a system like virtual memory (especially in Linux/*nix).

OP: You will find pointers in C to be the best thing in the world as well as the bane of your existence. Pointer: Know them, love them, despise them.

Also, as convention in C when making strings, you should write them out this way:
Code:
char string1[] = "Hello";
As this clearly states that you want to make a string and won't cause as much confusion as the way your wrote it out (even though it is completely valid).
Router
(12 items)
 
  
Reply
Router
(12 items)
 
  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C] How is data in an array passed?