LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2002, 06:22 AM   #1
painkiller6.6.6
LQ Newbie
 
Registered: Sep 2002
Posts: 7

Rep: Reputation: 0
simple c question


is "char ** mylist" equal to "char * mylist[]?
Cause I have char ** onelist and char * mylist[] and I have to assing this second list to the first.
thnx.
JP.
 
Old 09-25-2002, 07:32 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Yes, those two are essentially the same thing.

Alex
 
Old 09-25-2002, 08:28 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Compatible, yes, the same thing, no.

Here is a good example of why they are not the same:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
&nbsp;
int main(void)
{
  char  fixedSizePtr[10];
  char *dynamicSizePtr;
&nbsp;
  dynamicSizePtr = (char *)malloc(sizeof(char) * 10);
  if(dynamicSizePtr == NULL) {
    perror("Cannot malloc dynamicSizePtr");
    return 1;
  }
&nbsp;
/*  If you uncomment this portion, you will get a compiler error, because
   a C string (char []) pointer that is given a fixed size cannot be modified
&nbsp;
  fixedSizePtr = (char *)malloc(sizeof(char) * 10);
  if(fixedSizePtr == NULL) {
    perror("Cannot malloc fixedSizePtr");
    return 1;
  }
*/
&nbsp;
  strncpy(dynamicSizePtr, "Bacon!", 10);
  strncpy(fixedSizePtr, "Cheese!", 10);
&nbsp;
  printf("fixedSizePtr contains '%s' and is %d bytes in size\n",
      fixedSizePtr, sizeof(fixedSizePtr));
  printf("dynamicSizePtr contains '%s' and is %d bytes in size\n",
      dynamicSizePtr, sizeof(dynamicSizePtr));
&nbsp;
  free(dynamicSizePtr);
  return 0;
}
When run as is, it will return:

Quote:
~> ./test
fixedSizePtr contains 'Cheese!' and is 8 bytes in size
dynamicSizePtr contains 'Bacon!' and is 4 bytes in size
Notice, too, the size difference between the two? dynamicSizePtr was actually 10 characters long, but the sizeof returned 4.. because sizeof is returning the actual byte count of memory reserved for the variable, and in the case of dynamicSizePtr, it is only a memory location.

I don't know the specific terms for the differences, but this example shows one way they are different. They can be interchanged in a few different ways, however.
 
Old 09-25-2002, 08:36 AM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
I'm no expert on this, I just know enough to make me dangerous.. (=
 
Old 09-26-2002, 04:39 AM   #5
painkiller6.6.6
LQ Newbie
 
Registered: Sep 2002
Posts: 7

Original Poster
Rep: Reputation: 0
ok, thnx, it was just to rule out that error.
 
Old 09-30-2002, 04:29 PM   #6
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Re: simple c question

Quote:
Originally posted by painkiller6.6.6
is "char ** mylist" equal to "char * mylist[]?
Cause I have char ** onelist and char * mylist[] and I have to assing this second list to the first.
thnx.
JP.
In the first case mylist is a pointer to a pointer to char type, and in the other case mylist is an array of pointers to char type.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
Hopefully a Simple Question Arcane Kidd Linux - Newbie 5 01-17-2003 04:27 PM
Really Simple Question -- Please help! Cia420 Linux - General 3 05-07-2002 06:42 AM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration