Solved

Simple two dimensional array question

Posted on 2002-04-14
7
189 Views
Last Modified: 2010-04-15
Does this place a copy of the two dim pointer on the stack or does it copy the entire two dim array on the stack (MSVC 6.0)?


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

void go2(char [][80]);
int main(int argc, char* argv[])
{
     char lineptr[5][80];
     strcpy(lineptr[0], "ron");
     go2(lineptr);
     printf("%s\n", lineptr[0]);
     return 0;
}

void go2(char arr[][80])
//void go2(char *arr[])
{
     strcpy(arr[0], "h");
     printf("%s\n", arr[0]);
     return;
}
0
Comment
Question by:ronandersen
7 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 6941278
arrays are always passed by pointer (address), the compiler wont make a copy regardless of the function signature. this goes for char* and char[] aswell, i.e. one, two... fifty dimension arrays are all passed by address
0
 

Expert Comment

by:indira_shukla
ID: 6941667
It will make a copy of the pointers instead of the arrays as by passing the name of the array, you pass the address and hence the pointer.

0
 
LVL 2

Expert Comment

by:Slordak
ID: 6948946
The C language, regardless of compiler, does not copy arrays (regardless of the number of dimensions) when they are passed as function parameters.  Instead, the compiler always places a pointer to the first element of the array on the stack (which can be casted a number of ways).

As an aside, you can force the compiler to make a copy of an array by passing a structure containing an array as an argument, as the C language does allow structs (and their contents) to be passed by value.  In this case, the compiler then has no choice but to copy the entire structure onto the stack (although for obvious reasons you wouldn't want to do this under normal circumstances).
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Expert Comment

by:rahat
ID: 6954641
I think Answers is already given....
0
 

Expert Comment

by:zingo
ID: 6980009
Actually I think the "lineptr[5][80]" array in main ends up on the stack as a local variable.

But when you pass it as a parameter to another function only a pointer to it is passed.
0
 

Expert Comment

by:C1AzadNizam1C
ID: 7145199
You can generate a pointer to the first element of an array by simply specifying the array name, without any index.For example, given

int sample[10];

you can generate a pointer to the first element by simply using the name sample. For example the following program fragment assigns p the address of the first element of sample:

int*p;
int sample[10];
p=sample;


So,therefore I would say it will make a copy of the pointers instead of the arrays.
Asad.  
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9479711
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept makerp's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question