Solved

Simple two dimensional array question

Posted on 2002-04-14
7
188 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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:Kdo
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

786 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