Solved

Simple two dimensional array question

Posted on 2002-04-14
7
191 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 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 162
How to design, and implement simple interface 5 141
List out all word 7 352
outlook office 365 8 172
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

739 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