Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple two dimensional array question

Posted on 2002-04-14
7
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Accepted Solution

by:
makerp earned 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 46

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

604 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